mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-08-02 01:22:31 -04: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
src/engine
|
@ -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…
Add table
Add a link
Reference in a new issue