异步请求功能永远不会完成执行

时间:2018-08-16 02:00:13

标签: javascript node.js asynchronous promise

我在AWS Lambda函数中有一个函数,该函数可以通过AWS Cloud9中的node命令正常运行,但是在Docker-Lambda本地运行时失败。有问题的功能在下面的console.logs中带有“!”前缀已成功记录。那些没有的人。

async function getCoworkerById(id) {
    console.log('!*several enter getCoworkerById');
    return new Promise((resolve, reject) => {
        console.log('!*several enter getCoworkerById Promise');
        request({
            url: 'https://spaces.nexudus.com/api/spaces/coworkers',
            qs: {
                Coworker_Id: id
            },
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': 'Basic ' + new Buffer(process.env.nexudusEmail + ':' + process.env.nexudusPassword, 'utf8').toString('base64')
            }
        }, (error, response, body) => {
            console.log("enter getCoworkerById 2");
            if (error) {
                console.log("enter getCoworkerById error");
                reject(error);
            } else {
                console.log("enter getCoworkerById response");
                if (response.statusCode === 200) {
                    console.log('recieved 200 from getcoworkerbyid')
                    resolve(JSON.parse(body).Records[0]);
                } else if (response.statusCode === 409) {
                    console.log('recieved 409 from getcoworkerbyid')
                    setTimeout(() => {
                        resolve(getCoworkerById(id));
                    }, response.headers['Retry-After'] * 1000);
                } else {
                    reject(response);
                }
            }
        });
    });
}

在此块中调用该函数,请注意我正在使用异步库:

const coworkerPromises = [];
async.eachLimit(coworkerIds, 3, (id, callback) => {
  console.log('!*several enter each limit');
  try {
    console.log('!*several enter each limit try');
    coworkerPromises.push(getCoworkerById(id));
    callback();
  } catch (e) {
    console.log('Error getting coworker by ID: ', e);
  }
});
try {
  Promise.all(coworkerPromises).then(coworkers => {
    console.log("Enter promise all");
    console.log(coworkers[coworkers.length - 1]);
  });
} catch (e) {
  console.log(e);
}

这个真的让我很困惑。为什么我们永远都不会在没有错误或其他任何内容的情况下到达函数的后半部分?

0 个答案:

没有答案