Debian Codegraph @

lib/es6-promise/promise/resolve.js

raw
1 import {
2 noop,
3 resolve as _resolve
4 } from '../-internal';
5
6 /**
7 `Promise.resolve` returns a promise that will become resolved with the
8 passed `value`. It is shorthand for the following:
9
10 ```javascript
11 let promise = new Promise(function(resolve, reject){
12 resolve(1);
13 });
14
15 promise.then(function(value){
16 // value === 1
17 });
18 ```
19
20 Instead of writing the above, your code now simply becomes the following:
21
22 ```javascript
23 let promise = Promise.resolve(1);
24
25 promise.then(function(value){
26 // value === 1
27 });
28 ```
29
30 @method resolve
31 @static
32 @param {Any} value value that the returned promise will be resolved with
33 Useful for tooling.
34 @return {Promise} a promise that will become fulfilled with the given
35 `value`
36 */
37 export default function resolve(object) {
38 /*jshint validthis:true */
39 let Constructor = this;
40
41 if (object && typeof object === 'object' && object.constructor === Constructor) {
42 return object;
43 }
44
45 let promise = new Constructor(noop);
46 _resolve(promise, object);
47 return promise;
48 }