成日諗唔明點解 promise 點解要放 resolve/reject 做 define function 既 parameter

const promise = new Promise((resolve, reject) => {
  const result = performAsyncOperation();

  if (result) {
    resolve(result);
  } else {
    reject("Async operation failed");
  }
});

你要記住你成堆由 (resolve, reject)=>{xxx} 成舊野(your custom defined function) 都係叫 executor function

因為 promise 個 class pre-set 左 有一句會 call executor(resolve, reject)

既然你明知人地會 call 一個 function 要入 parameter resolve, reject 你創建個個 function 就當然要有 resolve, reject; 唔係人地 call 個陣咪唔夠料囉

你睇下 promise d src code 就知 你 define 塞入去個堆 logic 佢會叫做 executor

class Promise {
  constructor(executor) {
    this.state = 'pending';
    this.value = undefined;
    this.reason = undefined;
    this.callbacks = [];

    const resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fulfilled';
        this.value = value;
        this.callbacks.forEach((callback) => callback.onFulfilled(value));
      }
    };

    const reject = (reason) => {
      if (this.state === 'pending') {
        this.state = 'rejected';
        this.reason = reason;
        this.callbacks.forEach((callback) => callback.onRejected(reason));
      }
    };

    try {
      executor(resolve, reject);
    } catch (error) {
      reject(error);
    }
  }

  then(onFulfilled, onRejected) {
    if (typeof onFulfilled !== 'function') {
      onFulfilled = (value) => value;
    }
    if (typeof onRejected !== 'function') {
      onRejected = (reason) => {
        throw reason;
      };
    }

  === more promise method below ... ===

好啦 係 code 層面你明 , 人地叫呢個 resolve, reject 係 signal 囉

async 收到 signal (你 個 self define executor function call resolve 個下) 就會 call 用 .then register 既 callback …