Skip to content

Commit

Permalink
perf: avoid using new Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 23, 2021
1 parent f277e09 commit 5ea2ff6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
11 changes: 3 additions & 8 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,10 @@ class Hexo extends EventEmitter {
args = {};
}

return new Promise((resolve, reject) => {
const c = this.extend.console.get(name);
const c = this.extend.console.get(name);

if (c) {
Reflect.apply(c, this, [args]).then(resolve, reject);
} else {
reject(new Error(`Console \`${name}\` has not been registered yet!`));
}
}).asCallback(callback);
if (c) return Reflect.apply(c, this, [args]).asCallback(callback);
return Promise.reject(new Error(`Console \`${name}\` has not been registered yet!`));
}

model(name, schema) {
Expand Down
18 changes: 12 additions & 6 deletions lib/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ class Render {
const ctx = this.context;
let ext = '';

return new Promise((resolve, reject) => {
if (!data) return reject(new TypeError('No input file or string!'));
if (data.text != null) return resolve(data.text);
if (!data.path) return reject(new TypeError('No input file or string!'));
let promise;

readFile(data.path).then(resolve, reject);
}).then(text => {
if (!data) return Promise.reject(new TypeError('No input file or string!'));

if (data.text != null) {
promise = Promise.resolve(data.text);
} else if (!data.path) {
return Promise.reject(new TypeError('No input file or string!'));
} else {
promise = readFile(data.path);
}

return promise.then(text => {
data.text = text;
ext = data.engine || getExtname(data.path);
if (!ext || !this.isRenderable(ext)) return text;
Expand Down

0 comments on commit 5ea2ff6

Please sign in to comment.