So, I've been trying to run a promise in a sync way through a generator function. I need it this way, because I have to init a particular object before the rest of the dependencies, and the function that initializes all these dependencies has to return a particular app object, but not a promise (because the damn framework utilizes this object in a synchronous way afterwards).
So, the only small async part that I have to make sync is a ping call. Here's how I've made use of the generator:
function initLogger() {
const pingLogServer = function* (logServerHost) {
return ping.promise.probe(logServerHost)
.then((res) => {
return res;
});
};
const res = yield pingLogServer(logServerHost);
if (res.alive === true) {
... blah blah, do stuff to logger object
return logger;
}
Nevertheless, I get a syntax error:
const res = yield pingLogServer(logServerHost);
^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
It's the first time I'm implementing generators, and I used some tutorials (this and this), so I might as well be doing it wrong.
I'm on node v6.11.1
, so I can't use async/await
(unfortunately). If there are any other suggestions for implementing this - they're most welcome!
P.S. not a duplicate because it's a very narrowed-down problem/solution case, where I'm providing a big part of the solution and just looking for the end help, and I've explained why I can't use other solutions, f.e. async/await
.