0

Using seqalizerjs, resolve a promise:

return new Promise(function(resolve, reject){
            return models.merchants.create({
                name: ctx.name,
            });
        }).then(function(result){
            resolve({
                id: result.id
            });
        }).catch(function(err){
            reject(err);
        });

testing with chai as promise

return user.save(data).should.eventually.equal('123123');

but I always get this:

 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure
 it resolves.
Alvin
  • 8,219
  • 25
  • 96
  • 177

1 Answers1

0

I think the problem lies in this line:

return new Promise(function(resolve, reject){
        return models.merchants.create({
            name: ctx.name,
        });
        // .....

a Promise function should has resolve(), something like this:

return new Promise(function(resolve, reject){
        resolve(models.merchants.create({
            name: ctx.name,
        }));

Based on this reference:

The resolve and reject functions are bound to the promise and calling them fulfills or rejects the promise, respectively.

Still based on the reference, there is the Promise flow graph: enter image description here

The then() or catch() after Promise() doesn't handle the resolve() nor reject(), they only handle the value passed from either resolve() or reject().

So, I think your code should be something like this:

return new Promise(function(resolve, reject){
    resolve(models.merchants.create({
        name: ctx.name,
    }));
}).then(function(merchants){
    console.log(merchants) // the created `merchants` from the `resolve()` of the promise would be passed here.
    return merchants;
});

UPDATE

Your code should look like this:

return new Promise(function(resolve, reject){
    models.merchants.create({ // assuming `models.merchants.create()` is an async method
        name: ctx.name,
    }).then(function(result){
        resolve({
            id: result.id
        });
    }).catch(function(err){
        reject(err);
});
samAlvin
  • 1,648
  • 1
  • 11
  • 35