dimanche 29 mai 2016

EmberJS - the run loop is cleared with "asyn/await" instead of the use of "then"

I have a strange behavior with run loop in a test (yes, for everybody is difficult ;-) ) and with the babel plugin async to generator transform.

First, I have "an old school code":

await Ember.run(function() {
  return task.save().then(() => {
    subtask_1 = task.create_subtask();
    return subtask_1.save();
  }).then(() => {
    subtask_2 = task.create_subtask();
    return subtask_2.save();
  }).then(() => {
    subtask_3 = task.create_subtask();
    return subtask_3.save();
  }).then(() => {
    subtask_4 = task.create_subtask();
    return subtask_4.save();
  }).then(() => {
    return subtask_4.promise_priorize_before(subtask_2);
  });
});

It works. ;-)

But, if I tried, I think the equivalent code with the "new way", I have an error:

await Ember.run(async function() {
  console.log(Ember.run.currentRunLoop);
  await task.save();
  console.log(Ember.run.currentRunLoop);

  subtask_1 = task.create_subtask();
  console.log(Ember.run.currentRunLoop);
  await subtask_1.save();
  console.log(Ember.run.currentRunLoop); // <---- null here

  subtask_2 = task.create_subtask();     // <---- crash here
  console.log(Ember.run.currentRunLoop);
  await subtask_2.save();

  subtask_3 = task.create_subtask();
  await subtask_3.save();

  subtask_4 = task.create_subtask();
  await subtask_4.save();

  return subtask_4.promise_priorize_before(subtask_2);
});

I don't understand why, but the current run loop is cleared after a simple "save" of Ember data.

The error is "Assertion Failed: You have turned on testing mode,… any code with asynchronous side-effects in a run" because Ember.run.currentRunLoop is null.

Have you an idea?

Frédéric




Aucun commentaire:

Enregistrer un commentaire