Promise object (JavaScript)
In JavaScript, the promise object is used for asynchronous computations.
A Promise represents a value which may be available now, or in the future, or never.
A promise is composable, making it more complex than a callback function.
Description
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers to an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise for the value at some point in the future.
A Promise is in one of these states:
- pending: initial state, not fulfilled or rejected.
- fulfilled: meaning that the operation completed successfully.
- rejected: meaning that the operation failed.
A pending promise can either be fulfilled with a value, or rejected with a reason (error). When either of these happens, the associated handlers queued up by a promise's then method are called. (If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached.)
As the Promise.prototype.then() and Promise.prototype.catch() methods return promises, they can be chained.
Diagram
The diagram below illustrates the Promise object and related entities:
left|600px|thumb|Diagram illustrating the Promise object and related entities.
See also
- Bluebird (JavaScript library)
- Callback (computer programming)
- Futures and promises
- jQuery promise method
External links
- Native JavaScript promises API by Serg Hospodarets
- Promises | Web fundamentals - Promises have arrived natively in JavaScript
- Promise @ developer.mozilla.org
- Promise.all() @ developer.mozilla.org
- JavaScript Promise API by David Walsh
- JavaScript Promises by Jake Archibald
- ES6 Promises by Dave Atchley
- Overview of JavaScript Promises by Sandeep Panda
- Promises for asynchronous programming @ exploringjs.com
- Embracing Promises in JavaScript
- Using Javascript native promises, resolve a promise after thenables attached @ Stack Overflow
- Native Support for Promises in Node.js @ Stack Overflow
- Best Practices for Using Promises in JS by Oleksii Rudenko
- JavaScript Promises ... In Wicked Detail by Matt Greer
- How to Chain JavaScript Promises – Intro Tutorial to JavaScript Promises by Pablo Farias Navarro
- Native Javascript Promises vs Bluebird by Nazar Aziz
- Pure JS Promise (Native) by Stenly Tan
- Testing JavaScript's native Promises by Paul Meskers
- Promise Object (JavaScript) @ MSDN
- Promises Object @ JavaScript Kit
- Can I Use Promises? @ caniuse.com
- Hey Let's Learn About JavaScript Promises by Kevin Buchanan
- JavaScript Promises 101 by Ire Aderinokun
- Native Browser Promises In Javascript by Tomasz Maćkowiak
- Understanding JavaScript Promises, Pt. I: Background & Basics by Peleke Sengstacke
- Promise Objects - ECMAScript 2017 Language Specification
- The Simplest Thing Possible: Promises in JavaScript by John V. Petersen
- Learning ES6: Promises by Ben Ilegbodu
- TracedPromise: Visualizing Async JS Code by Ben Cronin
- On The Difficult Problem Of Logging Errors In Parallel Promises In JavaScript by Ben Nadel
- Easy asynchrony with ES6
- Staying Sane With Asynchronous Programming: Promises and Generators by Colin Toh
- Are JavaScript Promises swallowing your errors? by James K. Nelson
- ES6 Promises in Depth
- Promises – an alternative way to approach asynchronous JavaScript by Kishore Nallan
- Javascript Promises by Samy Pessé
- Better Asynchronous JavaScript by Francois Ward
- Good Code: Promises in JavaScript by Cecelia Wren
- Patterns for Asynchronous Programming With Promises
- Keeping our Promises (and Callbacks) by Thomas Bouldin
- Replacing async utilities with native promises by Jelle Kralt
- A Solution To Swallowed Exceptions In ES6’s Promises
- 8 Tips For Mastering Javascript Promises by Scott Rippey
- The best resources to learn about JavaScript Promises
- Native Promise versus jQuery Deferred by Adam Boduch @ jsfiddle
- Working With Javascript Promises: Practices And Mistakes @ by Adrian Oprea
- Promise-Based Validation by Pavan Podila
- Understanding JavaScript Promises @ spring.io
- Beginner's Guide to JavaScript promises
- What's so great about JavaScript Promises? by Bryan Klimt
- Introduction to ES6 Promises – The Four Functions You Need To Avoid Callback Hell by James K. Nelson
- JavaScript Promises – How They Work by Matt Behrens
- JavaScript Promises – How They Break by Matt Behrens
- JavaScript Promises and Error Handling by Scott Allen
- Understand promises before you start using async/await @ Daniel Brain
- Promise : Object
- The JavaScript Promise by Doguhan Uluca
- Chainable catches in a JavaScript promise by Peterbe.com
- Promise javascript examples by Kavit
- Make your life easier with JavaScript promises
- Making a Promise with JavaScript by Hemanth HM
- Javascript Promises by Pete Hodgson
- Implementing Promise.all and Promise.race in JavaScript by Edd Mann
- We promise you Promises: JavaScript Promises basics @ eKreative
- Aggregating Values In A Promise-Based Workflow In JavaScript by Ben Nadel
- Promises are the monad of asynchronous programming @ James Coglan
- A Simple Promise Implementation in about 20 lines of Javascript by Doug Turnbull
- JavaScript Promise API overview @ 1bytebeta.com
- JavaScript Promises Are Cool by Lou Mauget
- JavascriptPromise by Martin Fowler
- The Art of Promise-Based Architecture by Yuri Takhteyev
- Catch Errors in JavaScript Promise Chains by Taylor McGann
- Learn JavaScript promises in about 70 minutes by qntm
- Taming timeouts with a Promise by Paul Roberts
- Promises, Promises: Mastering Async I/O in Javascript with the Promise Pattern by Christian Lilley
YouTube videos
- How to Use Javascript Promises by Jordan Leigh
- JavaScript promises explained tutorial by techsmith
- Promises - Part 8 of Functional Programming in JavaScript
- Promises in Javascript by David M. Lee, II
- Cleaning Up JavaScript Callbacks With Promises
- Async JavaScript with Promises by Kyle Robinson Young
- Asynchronous JavaScript #4 - Promises by Net Ninja
- Introduction to Promises in JavaScript by Aidan Feldman