diff --git a/1-js/03-code-quality/02-coding-style/code-style.png b/1-js/03-code-quality/02-coding-style/code-style.png index b5f3ce8d..278fd294 100644 Binary files a/1-js/03-code-quality/02-coding-style/code-style.png and b/1-js/03-code-quality/02-coding-style/code-style.png differ diff --git a/1-js/03-code-quality/02-coding-style/code-style@2x.png b/1-js/03-code-quality/02-coding-style/code-style@2x.png index b60a4519..b5761434 100644 Binary files a/1-js/03-code-quality/02-coding-style/code-style@2x.png and b/1-js/03-code-quality/02-coding-style/code-style@2x.png differ diff --git a/8-async/03-promise-chaining/article.md b/8-async/03-promise-chaining/article.md index e69de29b..3706db0b 100644 --- a/8-async/03-promise-chaining/article.md +++ b/8-async/03-promise-chaining/article.md @@ -0,0 +1,84 @@ + +# Promises chaining + +There are many great things about promises. We're only starting. + +Now we'll cover promises chaining. They allow to build sequences of asynchronous actions. + +[cut] + +Here's the idea: + +```js run +new Promise(function(resolve, reject) { + // do a job... + setTimeout(() => resolve(1), 1000); +}).then(function(result) { + + alert(result); // 1 + return result * 2; + +}).then(function(result) { + + alert(result); // 2 + return result * 2; + +}).then(function(result) { + + alert(result); // 4 + return result * 2; + +}); +// ... +``` + +As you can see: + +- Calls to `.then` can be chained -- that's because `promise.then` returns a promise. +- A value returned by `.then` becomes a result in the next `.then`. + +If there's an error, it is also passed down the chain: + + +```js run +new Promise(function(resolve, reject) { + setTimeout(() => resolve(1), 1000); +}).then(function(result) { + + throw new Error("Woops!"); + +}).catch(function(error) { + + alert(error.message); // Woops! + +}); +``` + + + + +The idea is : + +- A callback in `.then` may return a result. + + +One of main purposes of promises is to make asyn +The main purpose of promises +Promises + +Promises can be chained. That allows actions to follow one after another. + +Here's a simple example first: + +```js +let promise = new Promise(function(resolve, reject) { + setTimeout(() => resolve("")) +}) + + + +What if we want to +The main idea behind promises +Promises can be used for asynchronous tasks that eventually finish with a result or an error. + +We already have `loadScript` diff --git a/figures.sketch b/figures.sketch index 3d7719f7..0c152845 100644 Binary files a/figures.sketch and b/figures.sketch differ