Node.js 每个回调问题的Aysnc

Node.js 每个回调问题的Aysnc,node.js,callback,node-async,Node.js,Callback,Node Async,我正在为每个任务使用async。我面临的问题是,最终的回调永远不会执行 场景:我有联系人列表,希望同时向所有联系人发送消息,并且在发送消息时希望将响应存储在数组中,而不是希望在最后回拨时执行某些操作 sms.js function SmsService() {} SmsService.prototype.sendSms = function(value, callback) { client.messages .create({

我正在为每个任务使用async。我面临的问题是,最终的回调永远不会执行

场景:我有联系人列表,希望同时向所有联系人发送消息,并且在发送消息时希望将响应存储在数组中,而不是希望在最后回拨时执行某些操作

sms.js

function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

       client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);

                callback(null,message.sid)
            })
            .catch(e => {

                callback(null,'not send')

            })
   }


module.exports = SmsService;
  var SmsService = require(path.resolve(__dirname, './sms'));
     var smsService = new SmsService();
     var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
     {body:'112123234',from:'+123123123',to:'+123213123'}, {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}]

     async.forEachOf(data, function (value, i, cb) {
        console.log('started',i)
            smsService.sendSms(value, function(error, result) {
                console.log('sending',i,value.to)//only get result for first item 
                results.push(result)
                cb()
            })

     }, function (err) {
        if (err) console.error(err.message);
         console.log('all done')//never executes
        console.log(results);//never executes
    });
function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

    return new Promise((resolve, reject) => {
        // Do async job
        client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);
                resolve(callback(null,message.sid))
            })
            .catch(e => {
                reject(callback(null,'not send'))
            })
    })
}
module.exports = SmsService;
var SmsService = require(path.resolve(__dirname, './sms'));
var smsService = new SmsService();
var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
            {body:'112123234',from:'+123123123',to:'+123213123'}, 
            {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}];
var promises = [];
data.forEach(async function (obj, index) {
    console.log('started',index)

    promises.push(await smsService.sendSms(obj, function(error, result) {
        console.log('sending',i,value.to)//only get result for first item 
        results.push(result)
    }));
});

Promise.all(promises).then(function () {
        console.log("Messages sent")
}).catch(function (err) {
        console.log("Messages not sent")
})
sender.js

function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

       client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);

                callback(null,message.sid)
            })
            .catch(e => {

                callback(null,'not send')

            })
   }


module.exports = SmsService;
  var SmsService = require(path.resolve(__dirname, './sms'));
     var smsService = new SmsService();
     var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
     {body:'112123234',from:'+123123123',to:'+123213123'}, {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}]

     async.forEachOf(data, function (value, i, cb) {
        console.log('started',i)
            smsService.sendSms(value, function(error, result) {
                console.log('sending',i,value.to)//only get result for first item 
                results.push(result)
                cb()
            })

     }, function (err) {
        if (err) console.error(err.message);
         console.log('all done')//never executes
        console.log(results);//never executes
    });
function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

    return new Promise((resolve, reject) => {
        // Do async job
        client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);
                resolve(callback(null,message.sid))
            })
            .catch(e => {
                reject(callback(null,'not send'))
            })
    })
}
module.exports = SmsService;
var SmsService = require(path.resolve(__dirname, './sms'));
var smsService = new SmsService();
var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
            {body:'112123234',from:'+123123123',to:'+123213123'}, 
            {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}];
var promises = [];
data.forEach(async function (obj, index) {
    console.log('started',index)

    promises.push(await smsService.sendSms(obj, function(error, result) {
        console.log('sending',i,value.to)//only get result for first item 
        results.push(result)
    }));
});

Promise.all(promises).then(function () {
        console.log("Messages sent")
}).catch(function (err) {
        console.log("Messages not sent")
})

如果我将异步部分移动到SMS服务,它可以正常工作,但我希望将SMS服务分开

您可以尝试类似的方法

sms.js

function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

       client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);

                callback(null,message.sid)
            })
            .catch(e => {

                callback(null,'not send')

            })
   }


module.exports = SmsService;
  var SmsService = require(path.resolve(__dirname, './sms'));
     var smsService = new SmsService();
     var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
     {body:'112123234',from:'+123123123',to:'+123213123'}, {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}]

     async.forEachOf(data, function (value, i, cb) {
        console.log('started',i)
            smsService.sendSms(value, function(error, result) {
                console.log('sending',i,value.to)//only get result for first item 
                results.push(result)
                cb()
            })

     }, function (err) {
        if (err) console.error(err.message);
         console.log('all done')//never executes
        console.log(results);//never executes
    });
function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

    return new Promise((resolve, reject) => {
        // Do async job
        client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);
                resolve(callback(null,message.sid))
            })
            .catch(e => {
                reject(callback(null,'not send'))
            })
    })
}
module.exports = SmsService;
var SmsService = require(path.resolve(__dirname, './sms'));
var smsService = new SmsService();
var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
            {body:'112123234',from:'+123123123',to:'+123213123'}, 
            {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}];
var promises = [];
data.forEach(async function (obj, index) {
    console.log('started',index)

    promises.push(await smsService.sendSms(obj, function(error, result) {
        console.log('sending',i,value.to)//only get result for first item 
        results.push(result)
    }));
});

Promise.all(promises).then(function () {
        console.log("Messages sent")
}).catch(function (err) {
        console.log("Messages not sent")
})
sender.js

function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

       client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);

                callback(null,message.sid)
            })
            .catch(e => {

                callback(null,'not send')

            })
   }


module.exports = SmsService;
  var SmsService = require(path.resolve(__dirname, './sms'));
     var smsService = new SmsService();
     var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
     {body:'112123234',from:'+123123123',to:'+123213123'}, {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}]

     async.forEachOf(data, function (value, i, cb) {
        console.log('started',i)
            smsService.sendSms(value, function(error, result) {
                console.log('sending',i,value.to)//only get result for first item 
                results.push(result)
                cb()
            })

     }, function (err) {
        if (err) console.error(err.message);
         console.log('all done')//never executes
        console.log(results);//never executes
    });
function SmsService() {}

SmsService.prototype.sendSms = function(value, callback) {

    return new Promise((resolve, reject) => {
        // Do async job
        client.messages
            .create({
                body: value.body,
                from: value.from,
                to: value.to
            })
            .then(message => {
                console.log('meesage going', message.sid);
                resolve(callback(null,message.sid))
            })
            .catch(e => {
                reject(callback(null,'not send'))
            })
    })
}
module.exports = SmsService;
var SmsService = require(path.resolve(__dirname, './sms'));
var smsService = new SmsService();
var data = [{body:'1232324',from:'+12323123',to:'+12312323'},
            {body:'112123234',from:'+123123123',to:'+123213123'}, 
            {body:'12sadasdasd34',from:'+112123123',to:'+1223213123'}];
var promises = [];
data.forEach(async function (obj, index) {
    console.log('started',index)

    promises.push(await smsService.sendSms(obj, function(error, result) {
        console.log('sending',i,value.to)//only get result for first item 
        results.push(result)
    }));
});

Promise.all(promises).then(function () {
        console.log("Messages sent")
}).catch(function (err) {
        console.log("Messages not sent")
})