Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
github.com/gurpartap/async
Context cancellable futures in Go
Provides a simple parallel execution primitive that we've all come to miss from Go: async/await futures.
Each future is a straightforward implementation of a single go routine and a cancellable result channel.
Accessing a future's result may require type casting from interface{}
.
go get -u github.com/Gurpartap/async
f := async.Any(...)
.v, err := f.Await()
or f.AwaitContext(ctx)
value := v.(*MyType)
.import (
"github.com/Gurpartap/async"
)
future := async.Any(func() (interface{}, error) {
return getUser() // a slow database or api call
})
// do other stuff until we need user
v, err := future.Await()
value := v.(*User)
// with timeout
timeout := 5*time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
v, err := future.Await()
if err != nil {
// handle if the err was a context timeout
}
value := v.(*User)
The async package provides a bunch future types, each useful for a varying number of return params:
For a value and an error:
func Any(func() (interface{}, error))
func Any(context.Context, func() (interface{}, error))
For 2 values and an error:
func Any2(func() (interface{}, interface{}, error))
func Any2(context.Context, func() (interface{}, interface{}, error))
For a value only:
func Value(func() interface{})
func Value(context.Context, func() interface{})
For an error only:
func Err(func() error)
func Err(context.Context, func() error)
package async_test
// ...
// see the complete example code in example_test.go
// ...
func ExampleSync() {
// if the db calls take 1 second each, the usual synchronous way
// will take 4 seconds to get results
user, err1 := db.GetUser()
name, age, err2 := db.GetNameAndAge()
location := db.GetLocation()
err := db.SetName("Another Name")
// use results
}
func ExampleAsync() {
// execute the db calls asynchronously
// and get results in 1 second
anyFuture := async.Any(func() (interface{}, error) {
return db.GetUser()
})
any2Future := async.Any2(func() (interface{}, interface{}, error) {
return db.GetNameAndAge()
})
valueFuture := async.Value(func() interface{} {
return db.GetLocation()
})
errFuture := async.Err(func() error {
return db.SetName("Another Name")
})
// wait for results
any, err1 := anyFuture.Await()
user := any.(User)
any1, any2, err2 := any2Future.Await()
name := any1.(string)
age := any2.(int)
value := valueFuture.Await()
location := value.(string)
// cancellable
ctx, _ := context.WithCancel(context.Background())
err := errFuture.AwaitContext(ctx)
// use results
}
Copyright 2018 Gurpartap Singh
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.