Skip to content

Instantly share code, notes, and snippets.

@thomasJang
Last active August 19, 2020 05:48
Show Gist options
  • Save thomasJang/ebef17c4fb31f450a8ac5b1705eb2589 to your computer and use it in GitHub Desktop.
Save thomasJang/ebef17c4fb31f450a8ac5b1705eb2589 to your computer and use it in GitHub Desktop.
asyncPromise.js
axisj.promise = function () {
// 프라미스
var myClass = function () {
this.busy = false;
this.queue = [];
this.then = function (fn) {
this.queue.push(fn);
this.exec();
return this;
};
// 함수 실행자 (큐가 남아 있으면 실행합니다.)
this.exec = function (data) {
if (this.busy) return this; // 바쁘니까 다음에 다시 봅시다.
var Q = this.queue.shift(),
self = this;
if (Q) {
this.busy = true;
try {
// 큐에 함수를 실행 인자는 ok, err, data 로
Q(
function (a) {
self.busy = false;
self.exec(a);
},
function (e) {
self._catch(e);
},
data
);
}
catch (e) {
this._catch(e);
}
} else {
this.busy = false;
}
};
// 에러가 발생되면.. 호출 하려고 둠.
this.catch = function (fn) {
this._catch = fn;
};
};
// 클래스 인스턴스를 반환합니다.
return new myClass();
}
@thomasJang
Copy link
Author

thomasJang commented Feb 2, 2017

axboot.promise()
  .then(function (ok, fail) {
      sqlgate.ajax({
          type: "PUT",
          url: "/api/v1/connections",
          data: JSON.stringify([data]),
          callback: function (res) {
              ok();
          },
          options: {
              nomask: true,
              onError: function (res) {
                  // console.log(res);
                  fail(res);
              }
          }
      });
  })
  .then(function (ok, fail) {
      sqlgate.ajax({
          type: "PUT",
          url: "/api/v1/session/open",
          data: JSON.stringify(data),
          callback: function (res) {
              ok();
          },
          options: {
              nomask: true,
              onError: function (res) {
                  console.log(res);
                  fail(res);
              }
          }
      });
  })
  .then(function (ok) {
      // 원하는 모듈의 ACTIONS 호출
      ACTIONS.dispatch(ACTIONS.EXECUTE, {target: "root.workspace.header", act: "GET_SESSION", data: {}});
      parent.sqlgate.modal.callback();
      ok();
  })
  .catch(function (res) {
      axWarningDialog.alert(res.message);
  });

@jomin398
Copy link

TypeError: Cannot find function _catch in object [object Object].
LineNuber : 35
How to define it?

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