NodeJs 瀑布回调功能不起作用
NodeJs waterfall callback functionality is not working
我正在使用 npm
async waterfall
方法编写功能。在那里我发现了一个奇怪的情况(可能是我意识低下)。我的功能如下。我在第一个函数中创建了一个事件数组,并通过回调参数将其传递给第二个函数。然后我在第二个函数中捕获该数组并循环它。我想操纵数组的几个值。之后我将它发送到最终函数。
final 函数中的 console.log
语句仅从第一个函数输出数组的初始状态,而不是我在第二个函数中所做的操作。任何想法都受到高度赞赏。
var _async = require('async');
var criteria = {};
_async.waterfall([
function getSortedCalenderItems(callback){
var events = []; // set of events are included inthis array
callback(null, events)
},
function getUsers(events, callback) {
for (var i = 0; i < events.length; i++) {
var event = events[i];
var user = event.shared_user;
// manipulate user
var newUser = {
'id' : '<id>',
'name' : 'New name'
};
event.shared_user = newUser;
events[i-1] = event;
if(i === (events.length)) {
callback(null, events)
}
}
}
],function(err, events){
console.log(events); // this shows the initial state of the array. Not the manipulated events array.
return;
});
例如;
var _async = require('async');
var criteria = {};
_async.waterfall([
function (callback) {
var events = [// set of events are included inthis array
{
name: "event1",
shared_user : "halil"
}, {
name: "event1",
shared_user : "halil"
}
];
callback(null, events);
},
function (events, callback) {
console.log(events)
for (var i = 0; i < events.length; i++) {
var event = events[i];
var user = event.shared_user;
// manipulate user
user = {
'id': '<id>',
'name': 'New name'
};
if (i === (events.length - 1)) {
callback({}, events)
}
}
}
], function (err, events) {
console.log(err, events);
});
我正在使用 npm
async waterfall
方法编写功能。在那里我发现了一个奇怪的情况(可能是我意识低下)。我的功能如下。我在第一个函数中创建了一个事件数组,并通过回调参数将其传递给第二个函数。然后我在第二个函数中捕获该数组并循环它。我想操纵数组的几个值。之后我将它发送到最终函数。
final 函数中的 console.log
语句仅从第一个函数输出数组的初始状态,而不是我在第二个函数中所做的操作。任何想法都受到高度赞赏。
var _async = require('async');
var criteria = {};
_async.waterfall([
function getSortedCalenderItems(callback){
var events = []; // set of events are included inthis array
callback(null, events)
},
function getUsers(events, callback) {
for (var i = 0; i < events.length; i++) {
var event = events[i];
var user = event.shared_user;
// manipulate user
var newUser = {
'id' : '<id>',
'name' : 'New name'
};
event.shared_user = newUser;
events[i-1] = event;
if(i === (events.length)) {
callback(null, events)
}
}
}
],function(err, events){
console.log(events); // this shows the initial state of the array. Not the manipulated events array.
return;
});
例如;
var _async = require('async');
var criteria = {};
_async.waterfall([
function (callback) {
var events = [// set of events are included inthis array
{
name: "event1",
shared_user : "halil"
}, {
name: "event1",
shared_user : "halil"
}
];
callback(null, events);
},
function (events, callback) {
console.log(events)
for (var i = 0; i < events.length; i++) {
var event = events[i];
var user = event.shared_user;
// manipulate user
user = {
'id': '<id>',
'name': 'New name'
};
if (i === (events.length - 1)) {
callback({}, events)
}
}
}
], function (err, events) {
console.log(err, events);
});