我試圖在我的lambda函數中設置順序異步調用。我爲此使用標準異步庫,並沒有成功運行所有控制檯打印。在Lambda中使用異步
// async import
var async = require('async');
exports.handler = (event, context, callback) => {
async.waterfall([
function func1(){
console.log('1');
},
function func2(){
console.log('2');
},
function func3(){
console.log('3');
},
], function (error, success) {
if (error) { console.log('Something is wrong!'); }
callback(null, 'success');
});
};
我看到的是:
START RequestId: b9dc249e-53ce-11e7-923a-95d7e896a384 Version: $LATEST
2017-06-18T02:35:00.915Z b9dc249e-53ce-11e7-923a-95d7e896a384 1
END RequestId: b9dc249e-53ce-11e7-923a-95d7e896a384
REPORT RequestId: b9dc249e-53ce-11e7-923a-95d7e896a384 Duration: 2.
任何幫助,非常感謝!
編輯:
萬一頂端回答不工作,這也適用:
var async = require('async');
exports.handler = (event, context, callback) => {
async.waterfall([
myFirstFunction,
mySecondFunction,
async.apply(myLastFunction, 'deen'),
], function (err, result) {
callback(null,'done');
});
};
function myFirstFunction(done) {
console.log('1');
done(null, 'one', 'two');
}
function mySecondFunction(arg1, arg2, done) {
// arg1 now equals 'one' and arg2 now equals 'two'
console.log('2');
done(null, 'three');
}
function myLastFunction(arg1, arg2, done) {
// arg1 is what you have passed in the apply function
// arg2 is from second function
console.log('3');
done(null, 'finished the functions');
}
你沒有錯,但這個傢伙上面首次在這裏與同樣的邏輯。 +1 – booky99