question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Destructuring bluebird Promise.all([..])

See original GitHub issue

Hi folks, I’m hoping someone can help with this. I have a fairly common pattern in my code of using array destructuring to name the results of await Promise.all([]). Example:

async function foo() {
  const [a, b] = await Promise.all([
    Promise.resolve(1),
    Promise.resolve('b'),
  ]);
}

(I’m using bluebird here). Running this through flow I get a bunch of errors that I don’t fully understand, but it looks like flow is not able to treat the awaited result of Promise.all([]) as a tuple?

foo.js:6
  6:   const [a, b] = await Promise.all([
                            ^ Bluebird$Promise. This type is incompatible with
606: declare function $await<T>(p: Promise<T> | T): T;
                                   ^^^^^^^^^^^^^^ union: type application of identifier `Promise` | type parameter `T` of await. See lib: /private/tmp/flow/flowlib_3ae7518b/core.js:606
  Member 1:
  606: declare function $await<T>(p: Promise<T> | T): T;
                                     ^^^^^^^^^^ type application of identifier `Promise`. See lib: /private/tmp/flow/flowlib_3ae7518b/core.js:606
  Error:
    6:   const [a, b] = await Promise.all([
                              ^ Bluebird$Promise. This type is incompatible with
  606: declare function $await<T>(p: Promise<T> | T): T;
                                     ^^^^^^^^^^ Promise. See lib: /private/tmp/flow/flowlib_3ae7518b/core.js:606
  Member 2:
    6:   const [a, b] = await Promise.all([
                        ^ type parameter `T` of await
  Error:
    6:   const [a, b] = await Promise.all([
                ^ element 0. Indexable signature not found in
    6:   const [a, b] = await Promise.all([
                        ^ Bluebird$Promise

Any help (or workarounds) would be much appreciated!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Dec 22, 2016

facebook/flow#2171

I use:

// @flow

declare function $await<T>(p: Bluebird$Promise<T> | T): T;
0reactions
jcreadycommented, Jan 27, 2017

The problem here is that an async function needs to be able to await any “thenable”. Here is an example of what should be accepted by flow:

/* @flow */

const thenable = { then: (cb) => cb('thenable') }

async function foo () {
  const a: string = await Promise.resolve('a')
  const b: string = await Promise.resolve(thenable)
  const c: string = await thenable
  return a + b + c
}

Here’s a basic “thenable” being awaited inside an async function on node 7 with the --harmony-async-await flag:

thenable

Read more comments on GitHub >

github_iconTop Results From Across the Web

.spread | bluebird
Bluebird is a fully featured JavaScript promises library with unmatched performance.
Read more >
Promise.all(...).spread is not a function when ... - Stack Overflow
I'll make my comment into an answer since it solved your issue. .spread() is not a standard promise method. It is available in...
Read more >
[Solved]-Promise.all(...).spread is not a function when running ...
You can include the Bluebird promise library so you have access to .spread() . var Promise = require('bluebird');. Use destructuring in the callback ......
Read more >
.spread() - Bluebird - W3cubDocs
all () but the ES6 destructuring syntax doesn't, hence the manual .all() call in the above code. If you want to coordinate several...
Read more >
JavaScript Async/Await Promise All Array Destructuring
I think you will agree with me when I say working with asynchronous code is an important part of modern app development.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found