我开始使用Node.对不起,这可能是一个愚蠢的问题.
试图理解为什么下面的代码会抛出错误:ReferenceError:未定义Promise
allAccountFixtures: ['account-customer-joe', 'account-partner-sam', 'account-partner-jane', 'account-admin-jill'], allProductFixtures: ['product-123', 'product-234', 'product-345', 'product-456'], ... loadBasicFixtures: (Api) => { return Promise.all([ Support.importRecords(Api.accountsAPI, Support.allAccountFixtures), Support.importRecords(Api.productsAPI, Support.allProductFixtures) ]); },
我的API在其他地方定义为:
this.accountsAPI = app.service('/api/accounts'); this.productsAPI = app.service('/api/products');
导入功能是:
importRecords: (feathersService, fixtureNames) => { // Wrap in an array if there's only one. if (!(fixtureNames instanceof Array)) { fixtureNames = [fixtureNames]; } // Create a separate promise for each JSON fixture to load the JSON from a // file and send it to feathers.create(). Don't execute yet. var promises = fixtureNames.map(fixtureName => { var filePath = `test/fixtures/json/${fixtureName}.json`; // console.log(`-> Loading JSON fixture: ${filePath}`); return fs.readFileAsync(filePath, 'utf8') .then((jsonString) => { return JSON.parse(jsonString); }).then((json) => { return feathersService.create(json); }); }); // Wrap all fixture loading promises inside a single outer promise that will // fire when all of the child promises are complete. return Promise.all(promises); },
不知道提供的信息是否足以告知正在发生的事情.我查了一个"承诺"的概念,这就是它.也许你可以指出正确的方向.文档提到了解决和拒绝.
我会对答案进行评论,因为它解决了您的问题.
一些较旧版本的node.js没有内置的promises,并且对它们使用promises需要加载一个增加了promise支持的第三方库.
如果升级到node.js或更新版本的任何4.x版本,则会在node.js中内置promises.