Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can someone help me understand why a library like this is even necessary? Can't you just wrap everything and treat it like a promise?

    const aPromise = Promise.resolve(1);
    const notAPromise = 2;

    Promise.resolve(aPromise).then((x) => console.log(x));
    Promise.resolve(notAPromise).then((y) => console.log(y));

    // Logs:
    // 1
    // 2


This is not a library. Stop thinking of it as a library. It's a building block, a module.

> why a library like this is even necessary?

Do you know how to determine whether something is a Promise?

Wrong. Also the first few StackOverflow answers are wrong or incomplete.

You know what's better? Using the same library 3.4 million repos depend on, that is tested and won't break if you use a package-lock.

> Can't you just wrap everything and treat it like a promise?

Maybe. Maybe not. Treating everything as a Promise means you have to make your function asynchronous even if not necessary.


Somehow convoluted, but if you wrap in a promise like this then you make it async (similar to setTimeout(fn, 0)), so in some situations you might want to keep the non-promised code as non-promise:

https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...

(red is async, blue is sync)


For compatibility with old versions of JS without Promise, when libraries used thenables or a promise library.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: