It seems like importing promise from core-js is fine, but then methods are not executing at all.
Is this a limitation of duktape (i.e. missing an event loop or something)?
Related
I am considering entire JS environment in two different parts in the question.
JS engine
Browser API, Node API (External to JS engine).
JavaScript engines(V8, SpiderMonkey) are single threaded and prior to ES6 there was no mechanism to run async code in JavaScript until ES6 introduced Promise concept.
I understand before promises came in, browsers or Node API(Server side JS) used to provide mechanism to run the code asynchronously using setTimeout and Process.nextTick and since Promises are natively supported to run async code in Javascript, I am trying to understand how promise callbacks and setTimeout are scheduled to run one before another.
Does this mean there are two event loops exists and they coordinate with each other? first in Browser/Node API to run code from setTimeout and Process.nextTick and another in JS engines to run promise callbacks, if no then how they are scheduled as there is no presence of setTimeout and Process.nextTick definition inside JS engines but Promise definition must be present in JS engines as Promise is ES6 standard.
Also I want to understand where is the task queue, job queue, mircotasks are present and managed, Inside JS engines or outside engine(in browsers or Node API).
the main two js runtime engine:
browser
nodejs(V8)
in the browser(google browser):
MacroTask:
setTimeout
setInterval
MicroTask:
Promise
execution sequence:
Promise> Promise.then> setTimeout
in nodejs runtime:
microtasks:
process.nextTick
promise
macrotasks:
setTimeout
setInterval
setImmediate
I/O
execution sequence:
promise> process.nextTick> promise.then > setImmediate> setTimeout
lots of articles about event-loop. give you some refs to help you know about the theory:
How JavaScript works: Event loop and the rise of Async programming + 5 ways to better coding with async/await
event-loop
event-loop-timers-and-nexttick
I followed the default example by Arangodbjs and it is seemed that in browser which does not support Promises, this one will fail. How can we work around this?
db.query(queryString).then()
From the documentation:
If you want to use promises in environments that don't provide the global Promise constructor, use a promise polyfill like es6-promise or inject a ES6-compatible promise implementation like bluebird into the global scope.
I use mongoose and bluebird as a promise framework.
Everytime I use "save" or "remove" I get this error :
Warning: a promise was created in a handler but was not returned from it
I really tried spending on it a few days, while googling, I tried so much ways, to mention some:
Creating a promise and resolve it in save/remove CB;
putting 'return' in so many logical combinations to make sure it
always 'return' from a promise.
Creating functions in the model, and naming it "saveAsync" (I saw it
in one example) and there doing all the promise handling.
Kinda funny but I tried updating all my project npm packages,
because I saw talks about it in github and someone mentioned they
solved it already. but it didn't work.
And much more.. I'm really desperate.
Don't get me wrong, the code works great, but seeing this HUGE warnings in my console each time makes me feel really guilty.
Any suggestions?
This error means some code did something like:
somePromise.then(x => {
someOtherPromiseReturningFunction();
}).then(value => {
// forgot a return, oh dear
});
Which is a very common mistake of forgetting a return, it messes with error handling and causes issues.
Sometimes the problem is not with your code but with the code of a library you're using - in that case you should disable warnings for that code:
Require bluebird separately for your own code and for mongoose via require("bluebird") and use it with warnings.
Disable warnings for the copy mongoose uses.
You can get two copies of bluebird by using require("bluebird") in your code and overriding mongoose's Promise with require("bluebird/js/release/promise")(); which creates a standalone copy.
I'm switching some code from bluebird to native Promises and am getting rather annoyed at the fact that native promises swallow errors even when there's not a .catch() defined. It makes debugging impossible unless you put a catch() on every promise.
So my question -- Does anyone have a solution to this? Possibilities include some way to tell promises to throw, or a way to globally catch them, or...?
I wrote and Petka (bluebird's author) implemented (we had help :)) this functionality for Node a little back. I don't think you should switch from bluebird (it's faster and has a richer API) but if you want - use the rejection hooks:
process.on('unhandledRejection', function(p, reason) {
// handle error here, all "swallowed" errors get here
});
This requires io.js 1.4+ or modern NodeJS (3.0+), this won't work in node 0.12, so better use a modern version or just keep using bluebird (which is also compatible with this event)
With CommonJS, require calls are synchronous, one can easily load a module dynamically like this:
require('./' + localModulePath);
ES6 introduces System.import which returns a Promise, and the standard import seems to not allow names that are determined at runtime. Is this a feature of CommonJS that is missing with ES6 modules or am I missing something?
Is this a feature of CommonJS that is missing with ES6 modules or am I missing something?
I don't think so. The fact that the module loading process is asynchronous allows you to use the same way in different environments. E.g. in a browser it wouldn't be possible to load the module synchronously, or at least we want to avoid it because synchronously fetching resources in JS is bad.
However, the import syntax gives you the impression of synchronous loading. It was a deliberate decision to make the statement statically analyzable so that environments can load all the dependencies before they excute the script, in which ever way they want.