我对nodejs很陌生。我创建了一个函数数组,但无法执行对函数的回调。有人可以帮我吗?
'using strict'
const tasks = [];
tasks.push(function (callback) {
console.log('Hi.. executing task 1');
return callback;
});
tasks.push(function (callback) {
console.log('Hi.. executing task 2');
return callback;
});
tasks.push(function (callback) {
console.log('Hi.. executing task 3');
return callback;
});
function iterate(index, callback) {
if (index === tasks.length) {
return callback();
}
const task = tasks[index];
task(function () {
console.log('Hi.. executing task callback');
//the line, I really want to include in here is
// iterate(index + 1, callback);
});
iterate(index + 1, callback);
}
function finish() {
console.log('Execution complete');
}
iterate(0, finish);
控制台显示
嗨,..执行任务1
嗨,..执行任务2
嗨,..执行任务3
执行完成
答案 0 :(得分:0)
如果您仔细查看它,您将不会执行回调,而只是返回它。所以你可以
import { gql } from "apollo-server-express";
import citySchema from "./city";
const linkSchema = gql`
type Query {
_: Boolean
}
type Mutation {
_: Boolean
}
type Subscription {
_: Boolean
}
`;
export default [linkSchema, citySchema];
或
cbFunc = task(function () {
console.log('Hi.. executing task callback');
//the line, I really want to include in here is
// iterate(index + 1, callback);
});
cbFunc();