Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upPromises based API #360
Promises based API #360
Comments
This comment has been minimized.
This comment has been minimized.
let torrent = yield client.add('leaves of grass.torrent')
let book = yield torrent.getBuffer('leaves of grass.epub')
read(book)Yeah, a promise API would be nice. |
This comment has been minimized.
This comment has been minimized.
|
Or like this client.download('<magnet-uri>')
.then(seed)
.catch(log.error); |
This comment has been minimized.
This comment has been minimized.
|
I don't use promises, but I'm willing to consider adding promises to WebTorrent if it's backwards compatible and doesn't add a ton of extra code. Rather than include a library like bluebird, I'd prefer to just use the built-in platform Promises, if they exist. PR welcome! |
This comment has been minimized.
This comment has been minimized.
|
jwalton/node-promise-breaker can be a good fit for the implementation. |
This comment has been minimized.
This comment has been minimized.
|
How var promise = require('bluebird');
function oldFunction(a, b, callback) {
if(a === b)
callback(); // Success
else
callback(new Error('a does not equal to b')); // Fail
}
function newFunction(a, b) {
return new promise(function(success, fail) {
oldFunction(a, b, function(error) {
if(error)
return fail(error);
return success();
});
});
}
function someFunctionUsingTheNewFunction(a, c) {
return newFunction(a, c+1);
}
function letsChainStuff() {
return newFunction(1, 1)
.then(function() {
return newFunction(2, 2);
}).then(function() {
return newFunction(3, 3);
}).then(function() {
return newFunction(4, 4);
}).then(function() {
return newFunction(1, 333); // This will fail
});
}
letsChainStuff()
.then(function() {
console.log('all done');
}).catch(function(error) {
console.error(error); // Some nasty error happened
});Notice that If you won't return a promise, a chain will continue directly to the next function. With bluebird promises it's also possible and very easy to make something execute in _series_ or in _parallel_. |
This comment has been minimized.
This comment has been minimized.
|
Sorry, we're not bundling |
This comment has been minimized.
This comment has been minimized.
|
I think the best way to get promises into webtorrent core is for someone who uses promises regularly (not me) to publish a new package |
This comment has been minimized.
This comment has been minimized.
|
In 2018 the JS promises are supported in the browsers and Node.js natively.
For example client.add(magnetURI).then(torrent => {
console.log(torrent.infoHash);
}).catch(error => {
console.error(error);
})instead of client.add(magnetURI, function(torrent) {
console.log(torrent.infoHash);
})and client.seed(files).then(torrent => {
console.log(torrent.infoHash);
}).catch(error => {
console.error(error);
})instead of client.seed(files, function(torrent) {
console.log(torrent.infoHash);
})and so on…
See how Firebase team has solved this problem: https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html. They added promises, but keep callbacks for backward compatibility. All functions in Firebase API now accept both Promise and callback-style methods. |
This comment has been minimized.
This comment has been minimized.
|
The ecosystem around Promises has matured quite a bit since I closed this issue. It's important to consider how to add Promises to the codebase without making the control flow hard to follow. I think I'd rather remove callbacks entirely to simplify maintenance, reduce the amount of code we need to test, etc. |
Webtorrent is something new.
Compared to events, promises are something new in node.js.
How about writing webtorrent using bluebird promises?
https://github.com/petkaantonov/bluebird#what-are-promises-and-why-should-i-use-them