使用js
中的eachOf
迭代asnyc framework数组,并在每次迭代中具有异步功能。如果调用了callback
并且未引发错误,则代码到达return语句。我想知道为什么回报被忽略并且没有立即离开承诺菊花链。如果我在eachOf
函数周围包装了另一个promise并解决了回调函数中的值,则第一层的promise将按预期完成。
为什么?回调函数中的返回值不足以结束该函数吗?
app1.js
const module1 = require('./module1');
const errorHandler = require('./error'); //implements custom error handler, e.g. log, mail, prowl, etc.
const values = {
val1: 'foo',
val2: 'bar',
};
return module1.getSomeVals()
.then(result => {
console.log('this is result:', result); //this returns undefined because the return in module1 not working
})
.catch(e => {
errorHandler.handle(e);
});
module1.js
const async = require('async'); //node module
const db = require('./db'); //custom db class
const module1 = {};
module1.getSomeVals= () => {
return new Promise((resolve, reject) => {
//some asyncronous function resolves values for next chain element, e.g. database stream
const resultFromSomeAsyncFunction = [
{
foo: 'foo1',
bar: 'bar1'
},
{
foo: 'foo2',
bar: 'bar2'
},
{
foo: 'foo3',
bar: 'bar3'
},
];
resolve({
val1: 'foo',
val2: 'bar',
});
})
.then(results => {
let newResults = [];
async.eachOf(results, (result, index, callback) => {
return db.query("select * from names where foo = '" + result.foo + "' and bar = '" + result.foo)
.then(rows => {
newResults.push(rows);
console.log('this is rows:', rows);
callback(null); //doesn't need second parameter, because newResults defined above
})
.catch(e => {
callback(e); //throw e works as well
});
},
(err) => {
if (err)
throw err;
else {
console.log('this is newResults:', newResults); //this is the new result as expected.
return newResults; //this return doesn't exit the function as expected and the next log will be reached
}
});
console.log('this is newResults and shouldn\'t reached at all:', newResults); //this line will be reached, but should'nt
});
};
module.exports = module1;
app2.js
const module2 = require('./module2');
const errorHandler = require('./error'); //implements custom error handler, e.g. log, mail, prowl, etc.
const values = {
val1: 'foo',
val2: 'bar',
};
return module2.getSomeVals()
.then(result => {
console.log('this is result:', result); //this returns the expected newResults array because the wrapped promise resolves the newResults
})
.catch(e => {
errorHandler.handle(e);
});
module2.js
const async = require('async'); //node module
const db = require('./db'); //custom db class
const module2 = {};
module2.getSomeVals = () => {
return new Promise((resolve, reject) => {
//some asyncronous function resolves values for next chain element, e.g. database stream
const resultFromSomeAsyncFunction = [
{
foo: 'foo1',
bar: 'bar1'
},
{
foo: 'foo2',
bar: 'bar2'
},
{
foo: 'foo3',
bar: 'bar3'
},
];
resolve({
val1: 'foo',
val2: 'bar',
});
})
.then(results => {
let newResults = [];
return new Promise((resolve, reject) => {
async.eachOf(results, (result, index, callback) => {
return db.query("select * from names where foo = '" + result.foo + "' and bar = '" + result.foo)
.then(rows => {
newResults.push(rows);
console.log('this is rows:', rows);
callback(null); //doesn't need second parameter, because newResults defined above
})
.catch(e => {
callback(e); //throw e works as well
});
},
(err) => {
if (err)
reject(err);
else {
console.log('this is newResults:', newResults); //this is the new result as expected.
resolve(newResults); //this resolve exit the function as expected and the next log wont be reached
}
});
});
console.log('this is newResults and shouldn\'t reached at all:', newResults); //this line wont be reached as expected
});
};
module.exports = module2;
Module2具有类似混乱的代码样式。我想在代码中有一个干净稳定的结构。
async eachOf的文档说:所有iteratee函数完成或发生错误时调用的回调。用(err)调用。
考虑到不再有像resolve
这样的回调函数,我除了返回应该以promise结束之外。我想了解一下,为什么这样的行为
我从asnyc框架中使用eachOf迭代了一个js数组,并且在每次迭代中都有一个异步函数。如果调用了回调并且未引发错误,则代码到达返回值...
在您的代码中: