You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone! First of all, thank you for the amazing work on this server. It's really great and extremelly fast to use!
I've a question about how can I use the type system to properly type a Promise-like table that I'm creating. Right now I need to type this piece of code:
localPromise=require".../promise"Promise:new(function(resolve, reject)
-- do some async stuff...ifsome_async_true_statusthenresolve(some_async_result)
elsereject(some_async_error)
endend):next(function(result) -- < `resolve()` value hereprint(result)
end):catch(function(err) -- < `reject()` value hereprint(err)
end)
This a Promise-like implementation that I'm doing for my neovim plugin which I'm trying to get a similar API as the Javascript Promise.
My question here is, how can I type this to the callee function knows what this is going to be returned from that callback? Currently, I'm trying to play around with @generic but, I can't get the server to properly understand what I mean by my types:
localPromise= {}
---@genericT---@classPromise<T>---@fieldnew(executor: fun(resolve: fun(result: T), reject: fun(err: any)))): Promise---@fieldnext(fn: fun(result: T)): Promise <<<<< here I'm returning `self`---@fieldcatch(fn: fun(err: any)): Promise <<<<< here I'm returning `self` as wellfunctionPromise:new(executor)
localpromise= { ... }
-- ... some operationsreturnsetmetatable(promise, {
__index=self,
})
endfunctionPromise:next(fn)
-- some operations...returnselfendfunctionPromise:catch(fn)
-- some operations...returnselfend------------------------ Here is how I want to use it:---@returnPromise<string>functionmy_function_that_return_promise()
returnPromise:new(function(resolve, reject)
-- .....resolve("hello")
end)
endmy_function_that_return_promise():next(function(result)
print(result) -- << here I want the server to show `result` is a `string`end)
For reference, this is how we can do this exactly thing using typescript:
constmyPromise=newPromise<string>((resolve,reject)=>{// ...resolve("hello")})myPromise.then((result)=>console.log(result))// < `result` will have `string` type.catch((err)=>console.log(err))
Thank you for the awesome work! Sorry if I didn't make myself clear, let me know if I need to provide any further information.
The text was updated successfully, but these errors were encountered:
carsakiller
changed the title
Help: How can I properly write the types (@class) for a promise-like table?
How do I document a generic class (JS Promises)
Nov 13, 2022
Hello everyone! First of all, thank you for the amazing work on this server. It's really great and extremelly fast to use!
I've a question about how can I use the type system to properly type a Promise-like table that I'm creating. Right now I need to type this piece of code:
This a Promise-like implementation that I'm doing for my neovim plugin which I'm trying to get a similar API as the Javascript Promise.
My question here is, how can I type this to the callee function knows what this is going to be returned from that callback? Currently, I'm trying to play around with
@generic
but, I can't get the server to properly understand what I mean by my types:For reference, this is how we can do this exactly thing using typescript:
Thank you for the awesome work! Sorry if I didn't make myself clear, let me know if I need to provide any further information.
The text was updated successfully, but these errors were encountered: