TimeMark开发日志

简洁记录一下开发过程和遇到的问题。

2018-1-16

完成了打包APK的步骤

2018-1-12

遇到一个问题,当我在前端POST数据到Express,如果6次没有返回值,那么Post就开始Pending了,所以每次POST都要返回一个Response。

查到一个解答https://stackoverflow.com/questions/2190854/sending-post-requests-without-waiting-for-response

You cannot just send data without receiving an answer with HTTP. HTTP always goes request -> response. Even if the response is just very short (like a simple 200 with no text), there needs to be a response. And every HTTP socket will wait for that response.

If you don’t care about the response, you could add a process to the server that makes your requests, and you just push your request data to it (like a service that is running in the background, checking a request database, and always starting the request whenever a new entry was added). That way you would make the request asynchronously and could quit as soon as you added that request to the stack.

Also as meouw said, the client is not part of any communication you are doing with php. Php is a server-side language, so when the client requests a webpage (the php file), the server executes that file (and does all requests the php file states) and then returns the result to the client.

2018-1-11

今天在做在主页获取Task的List。然后服务端遇到了传说中的回调地狱,简单来说。

在一个for循环中,执行一个异步函数。那么当执行完for循环后,异步函数还没执行。后面的代码如果有用到异步函数修改的内容,那么将无法获取正确的值。

解决方法两个,在回调函数里面加入一个counter,用于检测是否执行到最后一次回调。

方法二,用第三方库,我看了promise,还有些其他的。感觉promise不是很好懂。最后用async解决了问题,相关代码如下。

async.eachSeries(doc, function (key, callback) {
    templateMethod.findImageUrlByID(key.templateId,function (err, character){
        key._doc.imgUrl = character.imgUrl;
        callback(err);
    });
},function (err) {
    if (err) console.error(err.message);
    res.send({
        code: 200,
        msg: "Return task list",
        list: doc
    });
});

发表评论

邮箱地址不会被公开。