Moshe Shmukler
Moshe Shmukler

Reputation: 1300

Node - ReferenceError: Promise is not defined

I am starting out with Node. Sorry for what probably is a stupid question.

Trying to understand why the below code throws an error: ReferenceError: Promise is not defined

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)
   ]);
},

My APIs are defined elsewhere as:

this.accountsAPI = app.service('/api/accounts');
this.productsAPI = app.service('/api/products');

The import function is:

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);
},

Don't know whether the supplied information is sufficient to advise what is happening. I looked up the concept of a "promise" and that's pretty much it. Perhaps you could point to the right direction. The documentation mentions resolve and reject.

Upvotes: 4

Views: 7866

Answers (2)

jfriend00
jfriend00

Reputation: 707466

I'll make my comment into an answer since it solved your issue.

Some older versions of node.js do not have promises built-in and to use promises with them requires loading a third party library that adds promise support.

If you upgrade to any 4.x version of node.js or newer, you will have promises built-in to node.js.

Upvotes: 5

mcgraphix
mcgraphix

Reputation: 2733

You need to import and require Promise

npm install promise --save

Then

var Promise = require('promise');

Upvotes: 1

Related Questions