How to use rsvp

Comprehensive rsvp code examples:

How to use rsvp.hash:

1789
1790
1791
1792
1793
1794
1795
1796
1797
var init_recognizer = this.recognizer
  .then(this._initRecognizer.bind(this))
  .then(function(){ return this.recognizer; }.bind(this))
  .then(this._postMessage({ command: 'initialize' }));

var set_consumer = RSVP.hash({recorder: this.recorder, recognizer: this.recognizer})
  .then(function(results){
    var recorder   = results.recorder;
    var recognizer = results.recognizer;

How to use rsvp.cast:

11
12
13
14
15
16
17
18
19
}


InstallPhaseEvent.prototype.waitUntil = function (promise) {
    // FIXME: propagation? preventDefault?
    this.stopImmediatePropagation();
    this._wait = Promise.cast(promise);
    return this._wait;
};

How to use rsvp.reject:

10
11
12
13
14
15
16
17
18
19
20
}


AsyncMap.prototype.get = function (key) {
    key = key.toString();
    if (!this.has(key)) {
        return Promise.reject();
    }
    return Map.prototype.get.call(this, key);
};

How to use rsvp.defer:

1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
      });
    });


};
Sphinx.prototype._startRecognizer = function(){
  var dfd = RSVP.defer();


  var worker = new Worker(this.path_to_workers + '/recognizer.js');
  worker.onmessage = function() {
    dfd.resolve(worker);

How to use rsvp.all:

116
117
118
119
120
121
122
123
124
125
this.log('Release ' + response.version + ' exists.', {verbose: true});
this.log('Retrieving release files.', {verbose: true});
return this._getReleaseFiles().then(function(response) {
  if (this.readConfig('replaceFiles')) {
    this.log('Replacing files.', {verbose: true});
    return RSVP.all(response.map(this._deleteFile, this))
      .then(this._doUpload.bind(this))
      .then(this._logFiles.bind(this, response));
  } else {
    this.log('Leaving files alone.', {verbose: true});

How to use rsvp.EventTarget:

161
162
163
164
165
166
167
168
169

`EventTarget.mixin` also works with prototypes:

```javascript
var Person = function() {};
RSVP.EventTarget.mixin(Person.prototype);

var yehuda = new Person();
var tom = new Person();

How to use rsvp.denodeify:

8
9
10
11
12
13
14
15
16
17
18
let NpmAdapter = require('../../lib/dependency-manager-adapters/npm');
let writeJSONFile = require('../helpers/write-json-file');
let assertFileContainsJSON = require('../helpers/assert-file-contains-json');
let generateMockRun = require('../helpers/generate-mock-run');


let remove = RSVP.denodeify(fs.remove);
let root = process.cwd();
let tmproot = path.join(root, 'tmp');
let tmpdir;

How to use rsvp.allSettled:

105
106
107
108
109
110
111
112
113
114
var work = handlers.splice(0, handlers.length);

return RSVP.Promise.resolve(lastTime).
  then(function() {
    var firstRejected;
    return RSVP.allSettled(work.map(function(handler) {
      return RSVP.resolve(handler.call(null, code)).catch(function(e) {
        if (!firstRejected) {
          firstRejected = e;
        }

How to use rsvp.Promise:

165
166
167
168
169
170
171
172
173
174
_getFilesToUpload: function getFilesToUpload() {
  this.log('Generating file list for upload', {verbose: true});
  var dir = this.readConfig('distDir');
  var filePattern = this.readConfig('filePattern');
  var pattern = path.join(dir, filePattern);
  return new RSVP.Promise(function(resolve, reject) {
    // options is optional
    glob(pattern, function (err, files) {
      if(err) {
        reject(err);

How to use rsvp.resolve:

89
90
91
92
93
94
95
96
97
98
  },
  {
    command: 'npm --version',
    callback() {
      runCount++;
      return RSVP.resolve({ stdout: '5.7.1' });
    },
  },
],
{ allowPassthrough: false }