Arangodbjs query using with then() does not work IE11 - javascript

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.

Related

Is it possible to polyfill promises in Duktape?

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)?

Difference between Promise.all, webdriver.promise.all, protractor.promise.all

I have a protractor test in which I use protractor.promise.all to resolve several promises and it works fine. However, if I change to use Promise.all instead of protractor.promise.all I get some error. So I think there is a significant difference between them. My questions:
1) What is the difference between:
webdriver = require('selenium-webdriver');
webdriver.promise.all
and
protractor.promise.all
and
Promise.all
2) How can I find all the cases where I have to use protractors own implementation instead of the standard. Is there a documentation about these special cases?
Thank you!
protractor.promise provides you a quick access to the webdriver promise and is the same as webdriver.promise. But Promise is a built-in EcmaScript 2015 object for asynchronous computations.
In Protractor, there is this "Control Flow" mechanism that controls the queue of webdriver promises to resolve them in order and keep thing organized. If you want for your promises to be handled with Control Flow, use webdriver promises via protractor.promise.

Promise.all() work around for IE 11 with Backbone

Background: I have been tasked to help resolve an issue with the following error:
'Promise' is undefined'
Which is part of our sessionsmodel.js script:
return Promise.all(promises);
promises is an array of actions that need to happen and if any fail it is rejected.
Question: Based on my research IE does not support Promise so is there a work around that can be applied to this return value that will accomplish the same thing?
Since you are using Backbone, the promises are probably jQuery promises. You could use jQuery .when function to do the same as Promise.all:
return $.when.apply($, promises);
For most other simple situations where you call functions like save and fetch, you could avoid promises completely by using the provided callbacks:
model.save({
context: this,
success: this.onModelSuccess
});
There's no need to use another library because Backbone uses jQuery already (by default), unless you don't like jQuery deferred or that you're using something else in place of jQuery.
ES6 Promise spec was implemented by "good" libraries like Q, When, RSVP, Bluebird, Lie and more...
If you want to learn more on Promises, check this link:
Promises
I recommend you use a polyfill.

Resolve jQuery deferred with a promise

According to the promise resolution procedure of Promises/A+ specification it is
possible to resolve a deferred with a promise.
For example in Q library you just do:
deferred.resolve(promise);
Whether the implementation of promises bundled with jQuery supports this? I tried the same with jQuery and it treats promise just as an immediate value.
Whether the implementation of promises bundled with jQuery supports this?
No. jQuery is not Promise/A+-compliant.
However, notice that the spec is only about then and the promises returned by it, it does not mention Deferreds or how to resolve them. jQuery does indeed support assimilation of promises returned from a .then() callback (since V1.8, at least).

Mongoose promises documentation says queries are not promises?

From the docs (Mongoose v5.4.1, latest version):
Mongoose async operations, like .save() and queries, return
thenables.
This means that you can do things like MyModel.findOne({}).then()
second parapraph from the docs states:
Mongoose queries are not promises. They have a .then() function for co
and async/await as a convenience.
What Javascript MDN webpage states:
The then() method returns a Promise.
Does this mean that mongoose has another kind of implementation for async functions where they reserved the then keyword for the result of the async action?
In other words, they act like promises but are not JS promises?
From the documentation:
Mongoose queries are not promises. They have a .then() function for co
and async/await as a convenience. However, unlike promises, calling a
query's .then() can execute the query multiple times.
So unlike an actual promise, if you call then() multiple times on the query, you actually execute the query (or update) multiple times.
If you want an actual promise, call exec() on the query.
let promise = Test.findOne({}).exec();
All promises are thenables, but not all thenables are promises. To make things more complicated, not all promises are Promises (instances created by JavaScript's built-in Promise constructor).
JavaScript promises are an implementation of the Promises/A+ specification, which defines the terms like this:
1.1 “promise” is an object or function with a then method whose behavior conforms to this specification.
1.2 “thenable” is an object or function that defines a then method.
So Mongoose's queries are not promises, not even by that definition, since their then method is not compatible with the Promises/A+ spec. See JohnnyHK's answer for why they aren't compatible with the Promises/A+ spec (they run the query).
In other words, they act like promises but are not JS promises?
They only act a bit like promises. They are not promises. Their then is not implemented per the spec, it has side effects (running the query). If you want a true promise, see JohnnyHK's answer (e.g., use exec).
In general, if you have a thenable that's at least somewhat promise-like, you can get a proper promise for it by using Promise.resolve:
Promise.resolve(theThenable)
.then(/*...*/)
.catch(/*...*/)
.finally(/*...*/);
Promise.resolve will provide a true Promise instance that is resolved to the Mongoose thenable/promise: It will wait for that thenable/promise to settle and then settle the same way. That would work on a Mongoose query (provided you only do it once; exec is the better way with Mongoose queries).
They are "promise like", which means you can await them and call .then() and .catch() on them, however they are not instanceof Promise.

Categories

Resources