Cara menggunakan PROMISE.RESOLVE pada JavaScript

The Promise.resolve() static method "resolves" a given value to a Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0. If the value is a promise, that promise is returned; if the value is a , Promise.resolve() will call the Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 2 method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.

This function flattens nested layers of promise-like objects (e.g. a promise that fulfills to a promise that fulfills to something) into a single layer — a promise that fulfills to a non-thenable value.

Promise.resolve(value)

Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 3

Argument to be resolved by this Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0. Can also be a Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0 or a thenable to resolve.

A Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0 that is resolved with the given value, or the promise passed as value, if the value was a promise object. A resolved promise can be in any of the states — fulfilled, rejected, or pending. For example, resolving a rejected promise will still result in a rejected promise.

Promise.resolve() resolves a promise, which is not the same as fulfilling or rejecting the promise. See for definitions of the terminology. In brief, Promise.resolve() returns a promise whose eventual state depends on another promise, thenable object, or other value.

Promise.resolve() is generic and supports subclassing, which means it can be called on subclasses of Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0, and the result will be a promise of the subclass type. To do so, the subclass's constructor must implement the same signature as the const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 1 constructor — accepting a single const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 2 function that can be called with the const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 3 and const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 4 callbacks as parameters.

Promise.resolve() special-cases native Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0 instances. If Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 3 belongs to Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0 or a subclass, and const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 9, then Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 3 is directly returned by Promise.resolve(), without creating a new Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0 instance. Otherwise, Promise.resolve() is essentially a shorthand for const original = Promise.resolve(33); const cast = Promise.resolve(original); cast.then((value) => { console.log(`value: ${value}`); }); console.log(`original === cast ? ${original === cast}`); // Logs, in order: // original === cast ? true // value: 33 4.

The bulk of the resolving logic is actually implemented by the passed by the const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 1 constructor. In summary:

  • If a non- value is passed, the returned promise is already fulfilled with that value.
  • If a thenable is passed, the returned promise will adopt the state of that thenable by calling the const original = Promise.resolve(33); const cast = Promise.resolve(original); cast.then((value) => { console.log(`value: ${value}`); }); console.log(`original === cast ? ${original === cast}`); // Logs, in order: // original === cast ? true // value: 33 6 method and passing a pair of resolving functions as arguments. (But because native promises directly pass through Promise.resolve() without creating a wrapper, the const original = Promise.resolve(33); const cast = Promise.resolve(original); cast.then((value) => { console.log(`value: ${value}`); }); console.log(`original === cast ? ${original === cast}`); // Logs, in order: // original === cast ? true // value: 33 6 method is not called on native promises.) If the resolver function receives another thenable object, it will be resolved again, so that the eventual fulfillment value of the promise will never be thenable.

Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, );

const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 });

Promise.resolve() reuses existing Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 0 instances. If it's resolving a native promise, it returns the same promise instance without creating a wrapper.

const original = Promise.resolve(33); const cast = Promise.resolve(original); cast.then((value) => { console.log(`value: ${value}`); }); console.log(`original === cast ? ${original === cast}`); // Logs, in order: // original === cast ? true // value: 33

The inverted order of the logs is due to the fact that the const original = Promise.resolve(33); const cast = Promise.resolve(original); cast.then((value) => { console.log(`value: ${value}`); }); console.log(`original === cast ? ${original === cast}`); // Logs, in order: // original === cast ? true // value: 33 6 handlers are called asynchronously. See the Promise.resolve("Success").then( (value) => { console.log(value); // "Success" }, (reason) => { // not called }, ); 2 reference for more information.

// Resolving a thenable object const p1 = Promise.resolve({ then(onFulfill, onReject) { onFulfill("fulfilled!"); }, }); console.log(p1 instanceof Promise); // true, object casted to a Promise p1.then( (v) => { console.log(v); // "fulfilled!" }, (e) => { // not called }, ); // Thenable throws before callback // Promise rejects const thenable = { then(onFulfilled) { throw new TypeError("Throwing"); onFulfilled("Resolving"); }, }; const p2 = Promise.resolve(thenable); p2.then( (v) => { // not called }, (e) => { console.error(e); // TypeError: Throwing }, ); // Thenable throws after callback // Promise resolves const thenable = { then(onFulfilled) { onFulfilled("Resolving"); throw new TypeError("Throwing"); }, }; const p3 = Promise.resolve(thenable); p3.then( (v) => { console.log(v); // "Resolving" }, (e) => { // not called }, );

Nested thenables will be "deeply flattened" to a single promise.

const thenable = { then(onFulfilled, onRejected) { onFulfilled({ // The thenable is fulfilled with another thenable then(onFulfilled, onRejected) { onFulfilled(42); }, }); }, }; Promise.resolve(thenable).then((v) => { console.log(v); // 42 });

Warning: Do not call Promise.resolve() on a thenable that resolves to itself. That leads to infinite recursion, because it attempts to flatten an infinitely-nested promise.

const thenable = { then(onFulfilled, onRejected) { onFulfilled(thenable); }, }; Promise.resolve(thenable); // Will lead to infinite recursion.

Promise.resolve() is a generic method. It can be called on any constructor that implements the same signature as the const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 1 constructor. For example, we can call it on a constructor that passes it // Resolving a thenable object const p1 = Promise.resolve({ then(onFulfill, onReject) { onFulfill("fulfilled!"); }, }); console.log(p1 instanceof Promise); // true, object casted to a Promise p1.then( (v) => { console.log(v); // "fulfilled!" }, (e) => { // not called }, ); // Thenable throws before callback // Promise rejects const thenable = { then(onFulfilled) { throw new TypeError("Throwing"); onFulfilled("Resolving"); }, }; const p2 = Promise.resolve(thenable); p2.then( (v) => { // not called }, (e) => { console.error(e); // TypeError: Throwing }, ); // Thenable throws after callback // Promise resolves const thenable = { then(onFulfilled) { onFulfilled("Resolving"); throw new TypeError("Throwing"); }, }; const p3 = Promise.resolve(thenable); p3.then( (v) => { console.log(v); // "Resolving" }, (e) => { // not called }, ); 6 as const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 3:

class NotPromise { constructor(executor) { // The "resolve" and "reject" functions behave nothing like the // native promise's, but Promise.resolve() calls them in the same way. executor( (value) => console.log("Resolved", value), (reason) => console.log("Rejected", reason), ); } } Promise.resolve.call(NotPromise, "foo"); // Logs "Resolved foo"

The ability to flatten nested thenables is implemented by the resolver function of the const p = Promise.resolve([1, 2, 3]); p.then((v) => { console.log(v[0]); // 1 }); 1 constructor, so if you call it on another constructor, nested thenables may not be flattened, depending on how that constructor implements its resolver.

Apa Itu Promise di javascript?

Promise merupakan salah satu fitur yang penting dari ES6. Objek promise mewakili penyelesaian atau sebuah kesalahan pada operasi Asynchronous.

Kapan kita menggunakan asynchronous?

Proses asynchronous sering digunakan untuk komunikasi data, karena data menjadi bagian inti dari sebuah aplikasi maka konsep asynchronous sangat penting untuk dipahami.

Apa itu synchronous dan asynchronous pada javascript?

Definisi synchronous adalah sebuah operasi akan dijalankan setelah operasi sebelumnya selesai dijalankan alias berurutan. Sedangkan asynchronous sebaliknya, asynchronous tidak perlu menunggu operasi sebelumnya selesai untuk mengeksekusi operasi setelahnya.

Apa itu async Await Javascript?

Async/await adalah fitur yang hadir sejak ES2017. Fitur ini mempermudah kita dalam menangani proses asynchronous.Async/Await merupakan sebuah syntax khusus yang digunakan untuk menangani Promise agar penulisan code lebih efisien dan rapih.

Postingan terbaru

LIHAT SEMUA