mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-08 22:11:59 -05:00
Example that uses promises instead of util.report
This commit is contained in:
parent
8f6a88c095
commit
d72cc55c11
3 changed files with 29 additions and 14 deletions
|
@ -15,7 +15,8 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"htmlparser2": "3.9.0",
|
||||
"memoizee": "0.3.10"
|
||||
"memoizee": "0.3.10",
|
||||
"promise": "7.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "2.7.0",
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
var Promise = require('promise');
|
||||
|
||||
function Scratch3OperatorsBlocks(runtime) {
|
||||
/**
|
||||
* The runtime instantiating this block package.
|
||||
|
@ -81,10 +83,13 @@ Scratch3OperatorsBlocks.prototype.random = function (args, util) {
|
|||
// @todo Match Scratch 2.0 implementation with int-truncation.
|
||||
// See: http://bit.ly/1Qc0GzC
|
||||
util.yieldAndBlock();
|
||||
setTimeout(function() {
|
||||
var randomValue = (Math.random() * (args.TO - args.FROM)) + args.FROM;
|
||||
util.report(randomValue);
|
||||
}, 1000);
|
||||
var examplePromise = new Promise(function(resolve) {
|
||||
setTimeout(function() {
|
||||
var res = (Math.random() * (args.TO - args.FROM)) + args.FROM;
|
||||
resolve(res);
|
||||
}, 1000);
|
||||
});
|
||||
return examplePromise;
|
||||
};
|
||||
|
||||
module.exports = Scratch3OperatorsBlocks;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
var Promise = require('promise');
|
||||
var Thread = require('./thread');
|
||||
|
||||
/**
|
||||
|
@ -78,21 +79,29 @@ var execute = function (sequencer, thread) {
|
|||
done: function() {
|
||||
sequencer.proceedThread(thread);
|
||||
},
|
||||
report: function(reportedValue) {
|
||||
if (DEBUG_BLOCK_CALLS) {
|
||||
console.log('Reported: ', reportedValue);
|
||||
console.timeEnd('Yielding reporter evaluation');
|
||||
}
|
||||
thread.pushReportedValue(reportedValue);
|
||||
sequencer.proceedThread(thread);
|
||||
},
|
||||
timeout: thread.addTimeout.bind(thread),
|
||||
stackFrame: currentStackFrame.executionContext,
|
||||
startSubstack: function (substackNum) {
|
||||
sequencer.stepToSubstack(thread, substackNum);
|
||||
}
|
||||
});
|
||||
if (thread.status === Thread.STATUS_RUNNING) {
|
||||
|
||||
// Deal with any reported value.
|
||||
// If it's a promise, wait until promise resolves.
|
||||
var isPromise = (
|
||||
primitiveReportedValue &&
|
||||
primitiveReportedValue.then &&
|
||||
typeof primitiveReportedValue.then === 'function'
|
||||
);
|
||||
if (isPromise) {
|
||||
primitiveReportedValue.then(function(resolvedValue) {
|
||||
if (DEBUG_BLOCK_CALLS) {
|
||||
console.log('reporting value: ', resolvedValue);
|
||||
}
|
||||
thread.pushReportedValue(resolvedValue);
|
||||
sequencer.proceedThread(thread);
|
||||
});
|
||||
} else if (thread.status === Thread.STATUS_RUNNING) {
|
||||
if (DEBUG_BLOCK_CALLS) {
|
||||
console.log('reporting value: ', primitiveReportedValue);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue