Sharing Top Content from the Angular-sphere.

The ES6 Promises – codeburst

  • Now you can use Promises in your JavaScript code without any additional library.This post is a very basic explanation of promises to get you started using them in your code.So, What exactly is a Promise?Promise gives us control over our async function and allows us to either resolve or reject an…
  • catch(err = console.log(‘The number is not smaller then 10’))When we resolve a Promise the handler registered on then will get fired and if we reject the request then handler registered on catch will be called.Let’s look at an example which run an async operationconst timeoutIn = (time) = { return…
  • then(res = console.log(`Resloved in ${res/1000} seconds`))We defined a function timeoutIn which takes an argument time and resolves the promise after the time passed in using setTimeout.
  • If any of the promises get rejected then Promise.all call its catch handler and stops the resolving.Here is an example of Promise.allconst timeoutIn = (time) = { return new Promise((resolve, reject) = { setTimeout(() = resolve(time), time) })}const timeoutInArr = [ timeoutIn(2000), timeoutIn(4000), .
  • then(values = { console.log(‘All the promises are resolved now you can run your code’, values // your code })We created an array of unresolved promises and passed it into the Promise.all method once all the promises get resolved we output the values on console.Here is a basic explanation of ES6…

Promises introduced in ES6 to improve handling of async operations. They are around for a long time but with ES6 they became part of vanilla JavaScript. Now you can use Promises in your JavaScript…

The ES6 PromisesA very helpful feature in ES6Promises introduced in ES6 to improve handling of async operations. They are around for a long time but with ES6 they became part of vanilla JavaScript. Now you can use Promises in your JavaScript code without any additional library.This post is a very basic explanation of promises to get you started using them in your code.So, What exactly is a Promise?Promise gives us control over our async function and allows us to either resolve or reject an operation request.Let’s understand them by example. Here I am creating a Promiseconst isSmallThenTen = (num) = { return new Promise((resolve, reject) = { if(num 10) { resolve(true) } else { reject(false) } })}We created a function which takes a number, if num 10 evaluates to true then the promise is resolved and true is returned. If the condition fails the Promise gets rejected and false is returned.Let’s take a look how we are going to use this function in our codeisSmallThenTen(9) .then(res = console.log(‘The number is smaller then 10’)) .catch(err = console.log(‘The number is not smaller then 10’))When we resolve a Promise the handler registered on then will get fired and if we reject the request then handler registered on catch will be called.Let’s look at an example which run an async operationconst timeoutIn = (time) = { return new Promise((resolve, reject) = { setTimeout(() = resolve(time), time) })}timeoutIn(2000) .then(res = console.log(`Resloved in ${res/1000} seconds`))We defined a function timeoutIn which takes an argument time and resolves the promise after…

The ES6 Promises – codeburst

Comments are closed, but trackbacks and pingbacks are open.