-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eachPage Promisified behaviour #69
Comments
Yep, your usage above is correct! It's admittedly a bit strange, but we wanted the promisification to be non-breaking. In the common case where you just want to fetch all pages, you can use the |
This looks broken to me on version 0.5.6
Result:
Hmm. The library isn't even at 1.0.0 yet, and callbacks are ancient history. Having non-standard promise behaviour is pretty bad. |
Is |
Yes. Well, no error that I can see.
…On Fri., 7 Sep. 2018, 12:38 am Evan Hahn, ***@***.***> wrote:
Is catch receiving the records despite there being no error? That *does*
seem like a bug.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#69 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AApZyFCZO9E-w_p4kIETNegK2N1R59Fzks5uYTNOgaJpZM4WC9R4>
.
|
@stevage Yep that's a bug, but the correct behavior in your code example would be for it to throw an error saying that
Or equivalently:
|
Yeah, it turns out |
Agree with non-standard promise behavior is a bad thing, doesn't improve developer experience. |
@stevage, what does |
Heh, I don't remember, this was a while ago! |
|
Since I see folks still interested.. Here is an implementation wrapper I use to have a "Paged Cursor" kind of behavior which is promised based. Here is the wrapper: function loadFromAirTablePagedCursor(base, table, options) {
const cursor = {};
const doneCallback = error => {
if (error) {
cursor.reject(error);
} else {
cursor.resolve();
}
};
const eachPageCallback = (records, fetchNextPage) => {
cursor.nextPage = () => {
return new Promise((res, rej) => {
cursor.reject = rej;
cursor.resolve = res;
fetchNextPage();
});
};
cursor.resolve(records);
};
cursor.nextPage = () => {
return new Promise((res, rej) => {
cursor.reject = rej;
cursor.resolve = res;
base(table)
.select(options)
.eachPage(eachPageCallback, doneCallback);
});
};
return cursor;
} And used like this: const base = new Airtable({
/*your appid + creds*/
});
const table = "Some Table";
// This is the AT options provided to the JS Client
const options = {
sort: "",
pageSize: "",
maxRecords: "",
filterByFormula: "",
/*other stuff*/
};
// No network on next line - just an ability to paginate using promises
// encapsulated in a "cursor"
const atPageCursor = loadFromAirTablePagedCursor(base, table, options);
try {
// nextPage return a promise that resolves to an array of Record objects.
let atResultsPage = await atPageCursor.nextPage();
while (atResultsPage && atResultsPage.length) {
// Process this page.
const records = atResultsPage;
records[0].get('Some Field'); // For example
atResultsPage = await atPageCursor.nextPage();
}
} catch (err) {
// Errors thrown from the nextPage call would be caught here.
} hth |
Can we get the docs updated? Just lost a couple of hours searching for this - as I'm sure many other people do. |
@kasrak Thanks for this work.
Could you also provide a snippet showing how the
eachPage
of aQuery
object behaves when promisifed?Since the promise versions are used when the last callback is omitted, for the eachPage version that would be the
done
method. (Although thecallbackArgIndex
is specified, it still points at the last argument which is thedone
callback).Is this the correct promisified usage of the
eachPage
function?If so, it is still a bit weird to have to use a callback and implement the iterator/accumulator. where the promise happinness cannot be used to handle the async io taking place.
perhaps a better API would return a cursor with a promisified "next" method on it.. (waving hands here)
Thanks Again!
The text was updated successfully, but these errors were encountered: