Skip to content

Instantly share code, notes, and snippets.

@thevinayysharma
Last active July 14, 2021 17:08
Show Gist options
  • Select an option

  • Save thevinayysharma/b4d066b8958bb3d63570a1ee8f4b2e29 to your computer and use it in GitHub Desktop.

Select an option

Save thevinayysharma/b4d066b8958bb3d63570a1ee8f4b2e29 to your computer and use it in GitHub Desktop.
Covers:
Promises, basics, rule of promises, links, Quiz:


Earlier days, Callback pyramid was the only thing for doing several asyn functionalities. 
Error handler need to be written for every function operation. With Promise we can have a single catch.

A Promise is a object that simplify deferred and asynchronous computations. 

Promise states: pending, resolved & rejected

proto : Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: undefined or result obtained

for: resolve()   //result is "undefined" => if nothing is returned from resolve() 

.then multiple chaining => Composition in Promises.

Promise.all([p1, p2, p3]) => executes promises in parallel => resolves values in the order of the initial array;


Async-await : Provides a better and scalable code.
-------------------------------------------------

CODEWALKS:

somePromise().catch(function (err) {
  // handle error
});

is same as 

somePromise().then(null, function (err) {
  // handle error
});

----------------------------------------------------


Some good Practices:

1. Always terminate promises or use "return" statement, to break segmented chains. Unterminated promise chains lead to uncaught promise rejections in most browsers.

Bonus of “always returning”: code is much easier to unit test.

2. Promise.all instead of forEach


Keyword:
Deferred => deferred represents the computation that results in the value.

Read more:
* [Promises-deferred](https://lucybain.com/blog/2016/js-promises-vs-deferred)
* [More on Promises](https://danlevy.net/tags/promises)
* [dev take on promises](https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html)
QUESTIONS

1.  write the output ?
type of console  
typeof console.log  //helps to print value on screen
typeof console.log()

Hint: console.log() always return undefined.

//object
//function
//undefined


2. Which of the following option(s) will console.log 42?

// Option #1:
Promise.resolve(42)
  .then(console.log())

// Option #2:
Promise.resolve(42)
  .then(console.log)

// Option #3:
Promise.resolve(42)
  .then(value => console.log(value))

// Option #4:
Promise.resolve(42)
  .then((x) => console.log())
  .then(console.log)

//2 and 3

3. find output ?

Promise.resolve('Success!')
  .then(() => {
    throw Error('Oh noes!')
  })
  .catch(error => {
    return 'actually, that worked'
  })
  .then(data => {
    throw Error('The fails!')
  })
  .catch(error => console.log(error.message))


Promise.resolve('Success!')
  .then(() => {
    throw Error('Oh noes!')
  })
  .catch(error => {
    return 'actually, that worked'
  })
  .catch(data => {
    throw Error('The fails!')
  })
  .catch(error => console.log(error.message))

catch just catches it doesn't returns a vaue


//the fails

4. This will print 'Sucess' or not  ?  or Correct the function by yourself...

Promise.resolve('Success!')
  .then(data => {
    data.toUpperCase()
  })
  .then(data => {
    console.log(data)
  })


5. What will be the output be ?

var p = new Promise((resolve, reject) => {
    reject(Error('The Fails!'))
  })
  .catch(error => console.log(error))
  .then(error => console.log(error))

  //the fails
  //undefined

6. How many times "The fails is printed' ?

var p = new Promise((resolve, reject) => {
    reject(Error('The Fails!'))
  })
  .catch(error => console.log(error.message))
  .catch(error => console.log(error.message))
//the fails

7.  Find the output ??
var p = new Promise((resolve, reject) => {
  reject(Error('The Fails!'))
})
p.catch(error => console.log(error.message))
p.catch(error => console.log(error.message))

works like event handler ...or say domevent listeners


8. Giess the output?
Promise.resolve('foo').then(Promise.resolve('bar')).then(function (result) {
  console.log(result);
});


//prints foo

//use a "return" , otherwise it see the middle function as null

Promise.resolve('foo').then(null).then(function (result) {
  console.log(result);
});

.then is supposed to take a function  => previos value fall through in 2nd... we get foo.

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