mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 06:47:03 +00:00
Added async-polling definition (#11827)
* Added definition for async-polling * async-polling tests with noImplicitAny
This commit is contained in:
committed by
Mohamed Hegazy
parent
3e31546e34
commit
7dbdbc0187
@@ -0,0 +1,64 @@
|
||||
/// <reference path="async-polling.d.ts" />
|
||||
|
||||
import * as AsyncPolling from "async-polling";
|
||||
|
||||
// Tests based on examples in https://github.com/cGuille/async-polling#readme
|
||||
|
||||
AsyncPolling(end => {
|
||||
end();
|
||||
}, 3000).run();
|
||||
|
||||
function someAsynchroneProcess(callback: (error?: Error, response?: any) => any): any {
|
||||
callback();
|
||||
}
|
||||
|
||||
let polling = AsyncPolling(end => {
|
||||
someAsynchroneProcess(function (error, response) {
|
||||
if (error) {
|
||||
end(error);
|
||||
return;
|
||||
}
|
||||
|
||||
end(null, response);
|
||||
});
|
||||
}, 3000);
|
||||
polling.on("error", (error: Error) => {});
|
||||
polling.on("result", (result: any) => {});
|
||||
polling.run();
|
||||
polling.stop();
|
||||
|
||||
AsyncPolling(function(end) {
|
||||
this.stop();
|
||||
end();
|
||||
}, 3000).run();
|
||||
|
||||
let i = 0;
|
||||
|
||||
polling = AsyncPolling(function(end) {
|
||||
++i;
|
||||
if (i === 3) {
|
||||
return end(new Error("i is " + i));
|
||||
}
|
||||
if (i >= 5) {
|
||||
this.stop();
|
||||
return end(null, `#${i} stop`);
|
||||
}
|
||||
end(null, `#${i} wait a second...`);
|
||||
}, 1000);
|
||||
|
||||
const eventNames: AsyncPolling.EventName[] = ["run", "start", "end", "schedule", "stop"];
|
||||
eventNames.forEach(eventName => {
|
||||
polling.on(eventName, () => {
|
||||
console.log("lifecycle:", eventName);
|
||||
});
|
||||
});
|
||||
|
||||
polling.on("result", (result: any) => {
|
||||
console.log("result:", result);
|
||||
});
|
||||
|
||||
polling.on("error", (error: Error) => {
|
||||
console.error("error:", error);
|
||||
});
|
||||
|
||||
polling.run();
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// Type definitions for AsyncPolling
|
||||
// Project: https://github.com/cGuille/async-polling
|
||||
// Definitions by: Zlatko Andonovski <https://github.com/Goldsmith42/>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
declare module "async-polling" {
|
||||
module AsyncPolling {
|
||||
export type EventName = "run"|"start"|"error"|"result"|"end"|"schedule"|"stop";
|
||||
}
|
||||
|
||||
function AsyncPolling<Result>(pollingFunc: (end: (err?: Error, result?: Result) => any) => any, delay: number): {
|
||||
run: () => any;
|
||||
stop: () => any;
|
||||
on: (eventName: AsyncPolling.EventName, listener: Function) => any;
|
||||
}
|
||||
|
||||
export = AsyncPolling;
|
||||
}
|
||||
Reference in New Issue
Block a user