@Mykle1 said in How to best process an [object Promise]?:
@ninjabreadman said in How to best process an [object Promise]?:
automagically
Absolutely the best term I have heard to date. :-)
:) Especially in this context! I’m only now starting to understand why it’s called a magic mirror… Whatever you guys manage to create, it seem to have been done by development magic…
But, anyway, my problem is surely just a lack of conceptual and technical knowledge.
In my node_helper.js I was tryign to do this:
const NodeHelper = require("node_helper");
const fs = require('fs');
const async = require('async');
const radar = require('radar-api'); // This API return improper JSON
module.exports = NodeHelper.create({
...
async function radarPing() {
console.log("ENTER (inside)");
try {
const ping = await radar(-8.20917,114.62177,-9.28715,115.71243);
console.log("GOT DATA: ", await ping);
return await ping;
} catch(error) {
console.log("API ERROR: ", error);
}
//return "None";
console.log("EXIT (inside)");
},
readData: function() {
var radarData = "";
radarData = radarPing();
console.log("The DATA:\n" + radarData);
...
but that ended up with some weird errors, like:
const ping = await radar(-8.20917,114.62177,-9.28715,115.71243);
^^^^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:528:28)
Since, I’ve only started to learn about Promises, sync/await, just 2 days ago, I must be missing a lot. Because all the examples I’ve seen, are using more or less this way of writing.
So how the eeek do I make sure that what is returned by the api/poll function, is actual data and not an [object Promise]
, and how can I handle the Promise { }
that is returned in advance?
I’m overwhelmed…