Search code examples
javascriptecmascript-6generatoryield

What is the user of task/perform in ES6 Generator Code?


I have an Ember application and somewhere in my project, I can see the following code written in ES6.

expandRow: task(function* (row, data, params){
    var $this = this;
    // Some code
    // ...
    // ...
    // const response = yield this.someServerCall(url, params);
    // ...
    // ...  
});

The above is called somewhere using

this.get('expandRow').perform(row, data, params);

Looking at the above, while I do have an idea about the Generator syntax in ES6, I am a bit confused about the use of "task" and "perform" specifically.

I could not find that documented anywhere. Also I could not find any custom definitions for those in my code. Where should I look to understand more about the syntax?


Solution

  • This is all documented in the task-function-syntax area on the ember docs

    http://ember-concurrency.com/docs/task-function-syntax/

    Example from the docs:

    pickRandomNumbers: task(function * () {
      let nums = [];
      for (let i = 0; i < 3; i++) {
        nums.push(Math.floor(Math.random() * 10));
      }
    
      this.set('status', `My favorite numbers: ${nums.join(', ')}`);
    }),
    

    The perform function is just a way to executing said task.