A question about promise

  node.js, question
User.registerAsync(new User(body), password)
 .then(user => crypto.randomBytesAsync(20))
 .then(buf => {
 Active token = user. _ id plus buf.toString('hex');
 Now () plus 24 * 3600 * 1000;
 Var link = config.URL plus' /#/account/login/' plus user.activeToken;
 mailer({
 to: body.username,
 Subject:' Welcome to sign up for Issac logistics',
 Html:' please click < a href= "'plus link plus'" target="_blank "> here < /a > activate'
 });
 return user.save();
 })
 . then(user => res.json({message: `sent mail to ${ user.username} please activate `}) within 24 hours following the mail prompt)
 .catch(err => next(err));

I want to get the parameter of the previous promise in the second then method, that is, user. Should I not return another promise here, but continue writing code directly in the previous then method, but I don’t think this nesting is very elegant and I don’t know what solution is there? I use bluebird here. I don’t know whether to use the. all method, because the promise chain here still has a certain logical sequence.

Is this what you want

I didn’t notice it before.crypto.randomBytesAsync(20)It’s an asynchronous call, the Promise returned, I checked bluebird’s API, which is similar to ES6′ s Promise, so it should probably be changed to this:

User.registerAsync(new User(body), password)
 .then(user => Promise.all([user, crypto.randomBytesAsync(20)]))
 .then(result => {
 const user = result[0];
 const buf = result[1];
 
 Active token = user. _ id plus buf.toString("hex ");
 Now () plus 24 * 3600 * 1000;
 Var link = config.URL plus "/#/account/login/" plus user.activeToken;
 mailer({
 to: body.username,
 Subject: "Welcome to sign up for the Issachar logistics terminal",
 Html: "please click < a href=\" "plus link plus" \" target=\"_blank\ "> here < /a > to activate"
 });
 return user.save();
 })
 . then(user => res.json({ message: `sent mail to ${user.username} please activate `}) within 24 hours following the mail prompt)
 .catch(err => next(err));