0

I realize this may sound completely obvious, and the answer being no, but then again I am confused over a specific statement from the Skulpt example located at the bottom of their website

Mostly, the four lines (of 60) that I find confusing are:

   var myPromise = Sk.misceval.asyncToPromise(function() {
       return Sk.importMainWithBody("<stdin>", false, prog, true);
   });
   myPromise.then(function(mod) {
       console.log('success');
   },
       function(err) {
       console.log(err.toString());
   });

What is the point of the return statement within the function? Can it even be accessed? I suppose they could point to the function afterwards, within the method, but why not just pass it as a parameter?

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
  • 3
    it really really depends on how the callee (your anonymous function) is called by the caller and what the caller does with the callee's returned value (and to an extent what the returned value actually is) - without seeing what `Sk.misceval.asyncToPromise` or `Sk.importMainWithBody` is doing it's hard to say for sure – Jaromanda X Aug 09 '18 at 06:36
  • i.e read the source code [asyncToPromise](https://github.com/skulpt/skulpt/blob/master/src/misceval.js#L886) and [importMainWithBody](https://github.com/skulpt/skulpt/blob/master/src/import.js#L552) if you want to understand why this works as it does – Jaromanda X Aug 09 '18 at 06:42
  • Why is the behaviour of a promise is declared after it's called. In this example, the behaviour is defined in the .then(), what happens if the promise calls resolve() before the .then() has been declared. Wouldnt it make more sense to define the behaviour of it being called, before its called? –  Aug 09 '18 at 07:49
  • 1
    I think you don't understand asynchrony at all – Jaromanda X Aug 09 '18 at 07:54
  • but this post (https://stackoverflow.com/questions/42118900/when-is-the-body-of-a-promise-executed) says that the body of a promise is executed immediately. The body of the promise is what calls resolve(), but the resolve method is defined in the myPromise.then(function(mod) {...} ...), so wouldnt it make more sense to define the behaviour of the resolve/reject methods before the body is executed (which calls them)? If im missing someone obvious here, a link would be nice. I've been reading https://developers.google.com/web/fundamentals/primers/promises –  Aug 09 '18 at 08:19
  • the resolve method is part of the promise constructor - you don't have one of those in your code - read the *documentation* for `asyncToPromise` [here](https://github.com/skulpt/skulpt/blob/master/src/misceval.js#L854) - the first 3 lines of your code simply *converts* anything that is *suspendable* into a Promise - I can't even begin to understand what you mean by *behaviour of the promise/resolve/reject* since your code simply *consumes* a promise made for you by the library you are using – Jaromanda X Aug 09 '18 at 08:30
  • I would also recommend reading [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) over Goggles attempt at documentation – Jaromanda X Aug 09 '18 at 08:36
  • If you attach a `.then` callback after the promise resolved, the callback will be called one tick later. – Jonas Wilms Aug 09 '18 at 13:53
  • @J.Doe You might want to read [this thread](https://stackoverflow.com/q/50413583/1048572) – Bergi Aug 09 '18 at 14:57
  • The Skulpt documentation is pretty well impenetrable and needs a considerable makeover. – Roamer-1888 Aug 09 '18 at 15:42

1 Answers1

0

The function that calls the anonymous function that returns the value probably returns a Promise, which then resolves to the value returned by the anonymous function, so mod in the chained function is what was returned. If the returned thing was a promise, then it will be the value which that promise resolved with.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151