0

my below code work fine and i can create some data on database with that, but after that i need to get latest inserted id from that to use in other place of code, but this function which exported return promise object and i can't assign variable as function result, for example:

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

return Promise object and i can't assign variable with returned result as

return result.userUniqueId;

i want to get result and print or assign variable as the result with this code:

console.log( controller.createNewUser( models.users ) );
tux-world
  • 2,680
  • 6
  • 24
  • 55
  • Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Nov 20 '16 at 18:33
  • @Bergi i read it newly, but i can't resolve problem – tux-world Nov 20 '16 at 18:36
  • You cannot. The result is only available asynchronously, you cannot return it from the function. Just use `controller.createNewUser( models.users ).then( console.log )` instead. – Bergi Nov 20 '16 at 18:36
  • Regarding the antipattern, just drop the lines `return new Promise((resolve, reject) => {` and `.then(value => resolve(value));`, and your code will work better. – Bergi Nov 20 '16 at 18:37
  • @Bergi `model.create` is promise sir, after drop that lines how can i get result? – tux-world Nov 20 '16 at 18:40
  • You've got a promise for the result. Wait for it using `then` with a callback. There's no way around that. – Bergi Nov 20 '16 at 18:41
  • @Bergi your mean is i can't assign variable as function result? – tux-world Nov 20 '16 at 18:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128581/discussion-between-tux-world-and-bergi). – tux-world Nov 20 '16 at 18:46
  • @Bergi with `controller.createNewUser(models.users).then(function (userId) { console.log(userId); });` code, i can get result but i can't assign variable as that, can i use generators to resolve this problem? – tux-world Nov 20 '16 at 18:46
  • `userId` is a variable. That's as good as it gets. No, you cannot use generators. You might want to have a look at `async`/`await` for nicer syntax. – Bergi Nov 20 '16 at 19:07

2 Answers2

1

yes, using coroutines and yield you can actually suspend the exeuction of a method and return the co function back to the caller, and caller can resume the execution at his will by using the co function object with an argument passing to the fraction of the routine - but there is no way for the routine to pass back a half-baked data to the caller, nor it makes meaning in this context: in your code, until you generate the unique IDs of the user, there is nothing to return to the caller, and the unique ID generation is asynchronous, and does not end until all the callback is executed. In short, the coroutine use case does not apply here. Hope this clarifies.

Gireesh Punathil
  • 1,344
  • 8
  • 18
0

Dissecting the promise semantics:

  • It is just a semantic beautification two or more asynchronous calls.
  • Returning from anywhere in the chain is neither valid, nor meaningful.
  • As @Bergi points out, returning from partially executed asynchronous calls breaks the promise chain. Precisely, the caller-callee relation bound through a return value does not make make a valid semantic when Promise is involved.
  • Way out #1: create and return promise'd transaction to the caller, and then manage the rest of the chain in the caller (ugly).

  • Way out #2: Visualize the promise as the semantic wrapper around the old asynchronous call chains, and handle the result processing in the leaf of the promise chain (recommended).

  • Bottom-line: Caller does not get any stake in a call, where the callee take part in an asynchronous call.

Hope this helps.

Gireesh Punathil
  • 1,344
  • 8
  • 18
  • I'm thinking about this link as `https://x.st/javascript-coroutines/` which its about `Coroutine Event Loops in Javascript` and think i can return result and assign variable with `co` or generation function, what are you think? – tux-world Nov 21 '16 at 08:57