0

I'm using Sequelize database orm and by this code as

model.create(
    {
        username: 'John'
    } ).then( function (result) {
    return result.userUniqueId;
} );

I can create new user in database and i can print userUniqueId inside of then().

after create user I want to use returned userUniqueId in other functions or only print that on console, but i can't return result.userUniqueId from that, for resolve this problem I'm trying to use Promise to get result then I deployed that to:

'use strict';

module.exports = {
    createNewUser: function (model) {
        return new Promise(
            function (resolve, reject) {
                model.create(
                    {
                        username: 'John'
                    } ).then( function (result) {
                    return result.userUniqueId;
                } );
            } )
            .then( function (result) {
                resolve( result.userUniqueId );
            } );
    },
};

but i get this result outside of createNewUser function as console.log( controller.createNewUser( models.users ) );

Result:

Promise { <pending> }

How can i resolve this problem?

UPDATE:

'use strict';

module.exports = {
    createNewUser: function (model) {
        return new Promise((resolve, reject) => {
            return model.create(
                {
                    username: 'John'
                })
                .then(function (result) {
                    return result.userUniqueId;
                })
                .then(value => resolve(value));
        });
    },
};

and

var userId = controller.createNewUser(models.users)
        .then(function (userId) {
            return userId;
        });

console.log(userId);

print Promise object

tux-world
  • 2,680
  • 6
  • 24
  • 55

2 Answers2

0

This isn't a problem. Aside from promises that are created resolved or rejected (directly with Promise.resolve() and Promise.reject() respectively), all Promises are created in "pending" state.

That state will change when the Promise's resolve() function is called.

As an aside, you fell into the Promise constructor antipattern. Instead of this:

return new Promise((resolve, reject) => {
  doSomething()
    .then(value => resolve(value));
});

Just do

return doSomething()

No need to wrap Promises with additional Promise constructors.

Graham
  • 7,431
  • 18
  • 59
  • 84
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Just as with any promise, you need to use then to access its result value:

controller.createNewUser( models.users )
    .then(function (userId) {
         // do something with the ID
    });

You can't access the value from the promise directly because the promise may not be resolved yet.

Also as Madara points out, you have fallen into the explicit promise construction antipattern. Get rid of the extraneous code:

module.exports = {
    createNewUser: function (model) {
         return model.create(
             {
                 username: 'John'
             } )
             .then( function (result) {
                 return result.userUniqueId;
             } );
    },
};
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • I'm sorry sir, i think i dont understand exactly whats your mean, after change my code to your code as `module.exports` i get Promise object and your code as `controller.createNewUser( models.users ) ...` dont resolve my problem because i must be use result inside `then()` and i want to assign variable as `createNewUser` function – tux-world Nov 20 '16 at 17:32
  • My post updated sir – tux-world Nov 20 '16 at 17:39
  • @tux-world Show us the code where you're trying to use the return value of `createNewUser`, and not just a single line. – JLRishe Nov 21 '16 at 04:51
  • My post updated sir, Thanks – tux-world Nov 21 '16 at 05:46
  • @tux-world There is no way to make your code work the way you have written it there. The whole reason promises are involved is that the value is not available right away. Your question here is an extremely common one, please see this for an explanation of the situation and how to approach it: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – JLRishe Nov 21 '16 at 06:02