Back to JavaScript
JavaScript
easy
mid

How would you write a function that returns a resolved Promise with a value?

Write a Promise-returning function that resolves with a value. Most common form: `function task() { return new Promise(res => setTimeout(() => res(value), ms)); }`. Or short: `return Promise.resolve(value)`. Real-world: wrap a non-Promise API (geolocation, FileReader, setTimeout) so it composes with async/await.

2 min read·~5 min to think through

Typical interview prompt: write something that returns a Promise resolving with a value.

Bare minimum

js
function task() {
  return Promise.resolve(42);
}
await task(); // 42

Promise.resolve(v) returns an already-fulfilled Promise. Good for normalizing values that might be sync or async.

With a delay

js
function wait(ms, value) {
  return new Promise((resolve) => setTimeout(() => resolve(value), ms));
}

await wait(500, "done");   // "done" after 500ms

Wrapping a callback API

The classic real use — turn a non-Promise API into one:

js
function readFile(path) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, "utf8", (err, data) => {
      if (err) reject(err);
      else resolve(data);
    });
  });
}

Wrapping geolocation

js
function getPosition(options) {
  return new Promise((resolve, reject) => {
    navigator.geolocation.getCurrentPosition(resolve, reject, options);
  });
}

const pos = await getPosition({ enableHighAccuracy: true });

Wrapping an Image

js
function loadImage(src) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.onload = () => resolve(img);
    img.onerror = () => reject(new Error("Image load failed: " + src));
    img.src = src;
  });
}

const img = await loadImage("hero.jpg");

Resolve after a condition

js
function waitFor(predicate, intervalMs = 50, timeoutMs = 5000) {
  return new Promise((resolve, reject) => {
    const started = Date.now();
    const tick = () => {
      if (predicate()) resolve();
      else if (Date.now() - started > timeoutMs) reject(new Error("timeout"));
      else setTimeout(tick, intervalMs);
    };
    tick();
  });
}

await waitFor(() => document.querySelector("#ready"));

Anti-pattern: wrapping an existing Promise

js
// Bad
function load() {
  return new Promise((resolve, reject) => {
    fetch(url).then(resolve, reject);
  });
}

// Good
function load() {
  return fetch(url);
}

If you already have a Promise, return it.

Once-only resolution

A Promise settles once. Multiple resolve calls are ignored. So this is safe:

js
function withTimeout(promise, ms) {
  return new Promise((resolve, reject) => {
    promise.then(resolve, reject);
    setTimeout(() => reject(new Error("timeout")), ms);
  });
}

The Promise rejects on timeout if the original hasn't settled.

Interview framing

"For a Promise that just resolves with a value, Promise.resolve(v) is the one-liner. For a delay, new Promise(res => setTimeout(() => res(v), ms)). The real-world need is wrapping callback APIs — fs.readFile, geolocation, Image load — so they compose with await. Don't wrap an existing Promise in a new one (anti-pattern). Promises settle once, so you can race a Promise against a timeout safely."

Follow-up questions

  • Wrap setTimeout in a Promise.
  • Implement a timeout helper.
  • Why is wrapping a Promise in new Promise an anti-pattern?

Common mistakes

  • Wrapping an existing Promise.
  • Calling resolve twice expecting two events.
  • Forgetting to reject on error in callback wrap.

Performance considerations

  • Promise allocation is cheap. setTimeout for delays adds 4ms minimum nesting on browsers.

Edge cases

  • Resolving with a Promise — it chains.
  • Executor errors throw synchronously → automatic reject.

Real-world examples

  • fs.promises wrappers, custom geolocation/Image/FileReader Promise wrappers, test sleep utilities.

Senior engineer discussion

Seniors instantly recognize the constructor anti-pattern and prefer composable helpers.

Related questions