JavaScript Promise

new Promise( /* executor */ function(resolve, reject) { ... } );

Single return

function testFunction() {
  return new Promise(function (resolve, reject) {
    resolve("test1", "test2");
  });
}

function run() {
  testFunction()
      .then(function (e) { console.log(arguments); })
      .catch(function (err) { console.log(err); });
}

async function run() {
  try {
    var response = await testFunction();
    console.log(response); // test1
  } catch (err) {
    console.log(err);
  }
}

run();

Array destructuring

function testFunction() {
  return new Promise(function(resolve, reject) {
          resolve([ "test1", "test2"] );
          });
}

async function run() {
  const [firstRes, secondRes] = await testFunction();
  console.log(firstRes, secondRes);
}

run();

ref

What Else?
inflearn react api server -50% ํ• ์ธ์ฟ ํฐ: 15108-f2af1e086101 buy me a coffee