Skip to content

Instantly share code, notes, and snippets.

@fterrier
Created February 10, 2014 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fterrier/8916751 to your computer and use it in GitHub Desktop.
Save fterrier/8916751 to your computer and use it in GitHub Desktop.
// d: initial data
// cs: success callback
// cf: failure callback
var flows = {
'get_likes': function(d, cs, cf) {
likes_api
.get_likes(d)
.then(cs, cf);
},
'read_set_read_remove_read_likes': function(d, cs, cf) {
likes_api
.get_likes_by_user(d, function(body) {
// make sure that the user has no likes
return body.likes.length === 0;
})
.then(function(d) {
likes_api.set_like_for_user_and_item(d, function(body) {
// we test that it's set
return
true
}, function(new_data) {
return new_data;
})
}, cf)
.then(function(d) {
likes_api.get_likes_by_user(d, function(body) {
logger.info("test", body);
return true;
}, function(new_data) {
return new_data;
})
}, cf)
.then(likes_api.remove_like_from_user_and_item, cf)
.then(cs, cf);
}
};
@philippkueng
Copy link

Ok, habe den Fehler gefunden. Da die Funktionen remove_like_from_user_and_item jeweils als promise implementiert sind, und sie hier als void Funktion aufgerufen werden, bricht der Fluss. Um das Problem zu behen, einfach ein return vornehin stellen, damit das Promise der Funktion wieder ge-returned wird. Dann tut's wieder. Habe dir ein korrektes Beispiel unten.

// d: initial data
// cs: success callback
// cf: failure callback
var flows = {
  'get_likes': function(d, cs, cf) {
    likes_api
      .get_likes(d)
      .then(cs, cf);
  },
  'read_set_read_remove_read_likes': function(d, cs, cf) {
    likes_api
      .get_likes_by_user(d, function(body) {
        // make sure that the user has no likes
        return body.likes.length === 0;
      })
      .then(function(temp_d) {
        return likes_api.set_like_for_user_and_item(temp_d, function(body) {
          // we test that it's set
          return true;
        }, function(new_data) {
          return new_data;
        })
      }, cf)
      .then(function(temp_d) {
        return likes_api.get_likes_by_user(temp_d, function(body) {
          logger.info("test", body);
          return true;
        }, function(new_data) {
          return new_data;
        })
      }, cf)
      .then(likes_api.remove_like_from_user_and_item, cf)
      .then(cs, cf);
  }
};

und der darauf folgende Output:

phil:migros-monitoring philippkung$ foreman start
12:52:11 worker.1 | started with pid 29050
12:52:11 web.1    | started with pid 29051
12:52:11 worker.1 | info: Running 2 Migros Product API flows on https://api.migros.ch
12:52:11 worker.1 | info: FLOW: get_likes, STEP: get_likes
12:52:11 web.1    | Listening on 5100
12:52:11 worker.1 | info: Logging success for FLOW: get_likes
12:52:11 worker.1 | info: FLOW: read_set_read_remove_read_likes, STEP: get_likes_by_user
12:52:12 worker.1 | info: FLOW: read_set_read_remove_read_likes, STEP: set_like_for_user_and_item
12:52:13 worker.1 | info: FLOW: read_set_read_remove_read_likes, STEP: get_likes_by_user
12:52:15 worker.1 | info: test likes=[]
12:52:15 worker.1 | info: FLOW: read_set_read_remove_read_likes, STEP: remove_like_from_user_and_item
12:52:16 worker.1 | info: Logging success for FLOW: read_set_read_remove_read_likes

@philippkueng
Copy link

Ah ja und d muss in diesem Fall umbenannt werden. temp_d in meinem Fall, da es sonst mit dem ersten aus irgend einem Grund kollidiert, was es nicht tun sollte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment