Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript How to migrate promise.spread syntax to async/await with destructuring

I'm cleaning up some sequelize code and the findOrCreate function returns a promise that requires spreading to get the actual result object.

I'd like to rewrite my code to use await instead and, given ES6 supports array destructuring I'd have thought that instead of

User.findOrCreate({ where: { mcId }, defaults }).spread((user, created) => {
  // do stuff
})

I'd just be able to do

const [user, created] = await User.findOrCreate({ where: { mcId }, defaults })

but alas that's not the case.

I get the error (intermediate value) is not iterable

Is there any special trick to doing this or is what I am trying to do just not possible?

like image 256
Dave Sag Avatar asked Apr 11 '18 02:04

Dave Sag


1 Answers

I'm using Sequelize v4.37.10 and this worked for me -

 const [address, created] = await models.Address.findOrCreate({ where: { pid: 123}, defaults});
like image 54
Kshateesh Avatar answered Oct 24 '22 21:10

Kshateesh