Menu

gitpiper

Q.js Cheat Sheet in March 2023

Last Updated: 18 March 2023

README.md

Creating promises (Q.promise)

Q.promise (ok, fail) =>
  asyncFunction ->
    if error
      fail new Error("Failure")
    else
      ok(data)

For arrays

promises = [saveDisk(), saveCloud()]

# When all succeeds, or *at least one* error
Q.all(promises).done ->
  alert "Saved"

# Same, but get the values
Q.all(promises).spread (a, b) ->
  alert "Result A:" + a
  alert "Result B:" + b

# When all either succeeds or errors
Q.allSettled(promises).done -> ...

Creating promises from Node

# Works like .call() or .apply()

Q.nfcall(FS.readFile, 'foo.txt', 'utf-8')
.then -> ...

Q.nfapply(FS.readFile, ['foo.txt', 'utf-8'])
.then -> ...

Q.npost(FS, 'readFile', ['foo.txt, 'utf-8'])
.then -> ...

Q.npost(FS, 'readFile', 'foo.txt, 'utf-8')
.then -> ...

readFile = Q.denodeify(FS.readFile)
readFile('foo.txt').then -> ...

Promises to Node async

createUser = (next) ->
  promiseMaker()
  .nodeify(next)

Promise sugars

# Shortcut for .then(ok, fail, progress)
promise
.then (data) ->
.catch (err) ->
.progress (percent) ->

Try

Q.try ->
promise()

.catch (e) ->
console.error “Oh well”, e


338+ more cheat sheets for you in March 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️