Skip to content

Instantly share code, notes, and snippets.

@ben196888
Last active September 20, 2020 00:57
Show Gist options
  • Save ben196888/c8b4646c6133a5a6e67d5e58e1b47739 to your computer and use it in GitHub Desktop.
Save ben196888/c8b4646c6133a5a6e67d5e58e1b47739 to your computer and use it in GitHub Desktop.

Promise Expression

A String expression of the Promise chain.

Promise Methods

Mapping promise methods to a string expression.

Criteria

Pipeline would always pass the result to the down stream.

Defined methods:

  • .then()
  • .catch()
  • .all()
  • .race()

TBD methods:

  • .finally()
  • .allSettled()

Symbols

  • > .then
  • # .catch
  • && .all
  • || .race
  • () punctuation

Precedence

The following table lists the precedence symbols. symbols are listed top to bottom, in descending precedence.

Precedence Symbol Description
1 () group the Promise chain
2 > # Promise then and catch
3 && || Promise All and Race

Example

functions: foo, bar, boo, far

Example 1: foo > bar > boo > far would convert to Javascript

foo()
  .then(bar)
  .then(boo)
  .then(far)

Example 2: foo > bar # boo > far would convert to Javascript

foo()
  .then(bar)
  .catch(boo)
    .then(far)

Example 3: foo && bar > boo > far would convert to Javascript

Promise.all([
  foo(),
  bar()
    .then(boo)
    .then(far)
])

Example 4: (foo && bar) > boo > far would convet to Javascript

Promise.all([
  foo(),
  bar()
])
  .then(boo)
  .then(far)

Example 5: (foo || bar) > boo # far would convert to Javascript

Promise.race([
  foo(),
  bar()
])
  .then(boo)
  .catch(far)

Example 6: (foo > bar) && boo # far would convert to Javascript

Promise.all([
  foo()
    .then(bar),
  boo()
])
  .catch(far)
@kuoabu
Copy link

kuoabu commented Sep 19, 2020

So symbols precedence looks like (high -> low):

  • ()
  • >, #
  • &&, ||

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment