v7 icon indicating copy to clipboard operation
v7 copied to clipboard

JS Promises

Open stan-kondrat opened this issue 9 years ago • 8 comments

V7 has support JS Promises?

stan-kondrat avatar Apr 21 '16 06:04 stan-kondrat

Not yet but it's being developed, stay tuned.

mkmik avatar Apr 21 '16 07:04 mkmik

Also the early async-await support would be equally awesome: http://tc39.github.io/ecmascript-asyncawait/

ghost avatar Apr 21 '16 08:04 ghost

AFAIK v7 is single-threaded at the moment, to be able to implement promises/timeouts it would need to use some threading model, right?

endel avatar Jun 11 '16 19:06 endel

no. promises have nothing to do with threading. there are pure js implementations of promises you can use . we intend to include an implementation as part of the v7 standard library. we want to make it small enough so it can work well on embedded platforms.

mkmik avatar Jun 11 '16 19:06 mkmik

for the record, pure JS promises usually require a way to postpone invocation to the next event loop iteration (i.e. setTimeout, setImmediate, process.nextTick()). V7 is just a JS interpreter and doesn't have a concept of event loop.

We do implement event loop and setTimeout for several platforms in https://github.com/cesanta/mongoose-iot.


As an analogy, V7 is for mongoose-iot, as V8 is for node.js. A relevant thread on StackOverflow:

http://stackoverflow.com/questions/12335222/settimeout-and-v8

setTimeout is not part of ECMA-262, it's implemented by the browsers. However, if you install Node.js (which is V8 + extras) you will get a command line setTimeout.

That said, just for the craic, I tried out https://github.com/stefanpenner/es6-promise, one of the many promise polyfills (minified with uglifyjs --compress --mangle -- es6-promise.js since they seem to no longer distribute the minified version themselves), and it seems to work just fine with V7.

That impl is about 6k in size and is not optimized for embedded, which is why we're working on making it smaller.

mkmik avatar Jun 12 '16 09:06 mkmik

this promise polyfill tries several postponing methods:

if (isNode) {
  scheduleFlush = useNextTick();
} else if (BrowserMutationObserver) {
  scheduleFlush = useMutationObserver();
} else if (isWorker) {
  scheduleFlush = useMessageChannel();
} else if (browserWindow === undefined && typeof require === 'function') {
  scheduleFlush = attemptVertx();
} else {
  scheduleFlush = useSetTimeout();
}

I wonder which one succeeds...

YurySolovyov avatar Jun 19 '16 09:06 YurySolovyov

as I said, none with plain v7 (as none would work in v8). setTimeout will work with mongoose-iot

mkmik avatar Jun 19 '16 09:06 mkmik

Consider using https://github.com/bluejava/zousan that is compliant, fast and very very small in comparison to other impls.

niwinz avatar Jul 28 '16 20:07 niwinz