Skip to Content
JavaScript with Promises
book

JavaScript with Promises

by Daniel Parker
June 2015
Intermediate to advanced
100 pages
2h 7m
English
O'Reilly Media, Inc.
Content preview from JavaScript with Promises

Chapter 3. Working with Standard Promises

We’ve covered the standard Promise API and some basic scenarios, but like any technology, that’s only part of the story. Now it’s time for scenarios you’ll encounter and techniques you can use while writing real-world applications.

The Async Ripple Effect

Async functions and promises are contagious. After you start using them they naturally spread through your code. When you have one async function, any code that calls that function now contains an async step. The process of other functions becoming async by extension creates a ripple effect that continues all the way through the call stack. This is shown in Example 3-1 using three functions. Look how the async ajax function forces the other functions to also be async.

Example 3-1. The async ripple effect
showPun().then(function () {
    console.log('Maybe I should stick to programming');
});

function showPun() {
    return getPun().then(function (pun) {
        console.log(pun);
    });
}

function getPun() {
    // Assume ajax() returns a promise that is eventually
    // fulfilled by json for {content: 'The pet store job was ruff!'}
    return ajax(/*someurl*/).then(function (json) {
        var pun = JSON.parse(json);
        return pun.content;
    });
}

// Console output:
// The pet store job was ruff!
// Maybe I should stick to programming

The work to retrieve and display a pun is divided into three functions: showPun, getPun, and ajax. The functions form a chain of promises that starts with ajax and ends with the object returned ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Async JavaScript

Async JavaScript

Trevor Burnham

Publisher Resources

ISBN: 9781491930779Errata Page